src/primitives.cc

changeset 629
b75c6cce02e2
parent 628
6b13e4c2e97b
child 630
42ec68fcad9e
child 675
450827da2376
equal deleted inserted replaced
628:6b13e4c2e97b 629:b75c6cce02e2
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013, 2014 Santeri Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <QDir>
20 #include <QRegExp>
21 #include <QFileDialog>
22 #include "document.h"
23 #include "gui.h"
24 #include "primitives.h"
25 #include "ui_makeprim.h"
26 #include "misc.h"
27 #include "colors.h"
28 #include "moc_primitives.cpp"
29
30 QList<PrimitiveCategory*> g_PrimitiveCategories;
31 QList<Primitive> g_primitives;
32 static PrimitiveScanner* g_activeScanner = null;
33 PrimitiveCategory* g_unmatched = null;
34
35 extern_cfg (String, ld_defaultname);
36 extern_cfg (String, ld_defaultuser);
37 extern_cfg (Int, ld_defaultlicense);
38
39 static const QStringList g_radialNameRoots =
40 {
41 "edge",
42 "cyli",
43 "disc",
44 "ndis",
45 "ring",
46 "con"
47 };
48
49 PrimitiveScanner* getActivePrimitiveScanner()
50 {
51 return g_activeScanner;
52 }
53
54 // =============================================================================
55 // -----------------------------------------------------------------------------
56 void loadPrimitives()
57 {
58 PrimitiveCategory::loadCategories();
59
60 // Try to load prims.cfg
61 QFile conf (Config::filepath ("prims.cfg"));
62
63 if (!conf.open (QIODevice::ReadOnly))
64 {
65 // No prims.cfg, build it
66 PrimitiveScanner::start();
67 }
68 else
69 {
70 while (conf.atEnd() == false)
71 {
72 QString line = conf.readLine();
73
74 if (line.endsWith ("\n"))
75 line.chop (1);
76
77 int space = line.indexOf (" ");
78
79 if (space == -1)
80 continue;
81
82 Primitive info;
83 info.name = line.left (space);
84 info.title = line.mid (space + 1);
85 g_primitives << info;
86 }
87
88 PrimitiveCategory::populateCategories();
89 log ("%1 primitives loaded.\n", g_primitives.size());
90 }
91 }
92
93 // =============================================================================
94 // -----------------------------------------------------------------------------
95 static void recursiveGetFilenames (QDir dir, QList<QString>& fnames)
96 {
97 QFileInfoList flist = dir.entryInfoList (QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
98
99 for (const QFileInfo& info : flist)
100 {
101 if (info.isDir())
102 recursiveGetFilenames (QDir (info.absoluteFilePath()), fnames);
103 else
104 fnames << info.absoluteFilePath();
105 }
106 }
107
108 // =============================================================================
109 // -----------------------------------------------------------------------------
110 PrimitiveScanner::PrimitiveScanner (QObject* parent) :
111 QObject (parent),
112 m_i (0)
113 {
114 g_activeScanner = this;
115 QDir dir (LDPaths::prims());
116 assert (dir.exists());
117 m_baselen = dir.absolutePath().length();
118 recursiveGetFilenames (dir, m_files);
119 emit starting (m_files.size());
120 log ("Scanning primitives...");
121 }
122
123 // =============================================================================
124 // -----------------------------------------------------------------------------
125 PrimitiveScanner::~PrimitiveScanner()
126 {
127 g_activeScanner = null;
128 }
129
130 // =============================================================================
131 // -----------------------------------------------------------------------------
132 void PrimitiveScanner::work()
133 {
134 int j = min (m_i + 100, m_files.size());
135
136 for (; m_i < j; ++m_i)
137 {
138 QString fname = m_files[m_i];
139 QFile f (fname);
140
141 if (!f.open (QIODevice::ReadOnly))
142 continue;
143
144 Primitive info;
145 info.name = fname.mid (m_baselen + 1); // make full path relative
146 info.name.replace ('/', '\\'); // use DOS backslashes, they're expected
147 info.cat = null;
148 QByteArray titledata = f.readLine();
149
150 if (titledata != QByteArray())
151 info.title = QString::fromUtf8 (titledata);
152
153 info.title = info.title.simplified();
154
155 if (Q_LIKELY (info.title[0] == '0'))
156 {
157 info.title.remove (0, 1); // remove 0
158 info.title = info.title.simplified();
159 }
160
161 m_prims << info;
162 }
163
164 if (m_i == m_files.size())
165 {
166 // Done with primitives, now save to a config file
167 QString path = Config::filepath ("prims.cfg");
168 QFile conf (path);
169
170 if (!conf.open (QIODevice::WriteOnly | QIODevice::Text))
171 critical (fmt ("Couldn't write primitive list %1: %2",
172 path, conf.errorString()));
173 else
174 {
175 for (Primitive& info : m_prims)
176 fprint (conf, "%1 %2\r\n", info.name, info.title);
177
178 conf.close();
179 }
180
181 g_primitives = m_prims;
182 PrimitiveCategory::populateCategories();
183 log ("%1 primitives scanned", g_primitives.size());
184 g_activeScanner = null;
185 emit workDone();
186 deleteLater();
187 }
188 else
189 {
190 // Defer to event loop, pick up the work later
191 emit update (m_i);
192 QMetaObject::invokeMethod (this, "work", Qt::QueuedConnection);
193 }
194 }
195
196 // =============================================================================
197 // -----------------------------------------------------------------------------
198 void PrimitiveScanner::start()
199 {
200 if (g_activeScanner)
201 return;
202
203 PrimitiveScanner* scanner = new PrimitiveScanner;
204 scanner->work();
205 }
206
207 // =============================================================================
208 // -----------------------------------------------------------------------------
209 PrimitiveCategory::PrimitiveCategory (QString name, QObject* parent) :
210 QObject (parent),
211 m_Name (name) {}
212
213 // =============================================================================
214 // -----------------------------------------------------------------------------
215 void PrimitiveCategory::populateCategories()
216 {
217 for (PrimitiveCategory* cat : g_PrimitiveCategories)
218 cat->prims.clear();
219
220
221 for (Primitive& prim : g_primitives)
222 {
223 bool matched = false;
224 prim.cat = null;
225
226 // Go over the categories and their regexes, if and when there's a match,
227 // the primitive's category is set to the category the regex beloings to.
228 for (PrimitiveCategory* cat : g_PrimitiveCategories)
229 {
230 for (RegexEntry& entry : cat->regexes)
231 {
232 switch (entry.type)
233 {
234 case EFilenameRegex:
235 {
236 // f-regex, check against filename
237 matched = entry.regex.exactMatch (prim.name);
238 } break;
239
240 case ETitleRegex:
241 {
242 // t-regex, check against title
243 matched = entry.regex.exactMatch (prim.title);
244 } break;
245 }
246
247 if (matched)
248 {
249 prim.cat = cat;
250 break;
251 }
252 }
253
254 // Drop out if a category was decided on.
255 if (prim.cat != null)
256 break;
257 }
258
259 // If there was a match, add the primitive to the category.
260 // Otherwise, add it to the list of unmatched primitives.
261 if (prim.cat != null)
262 prim.cat->prims << prim;
263 else
264 g_unmatched->prims << prim;
265 }
266 }
267
268 // =============================================================================
269 // -----------------------------------------------------------------------------
270 void PrimitiveCategory::loadCategories()
271 {
272 for (PrimitiveCategory* cat : g_PrimitiveCategories)
273 delete cat;
274
275 g_PrimitiveCategories.clear();
276 QString path = Config::dirpath() + "primregexps.cfg";
277
278 if (!QFile::exists (path))
279 path = ":/data/primitive-categories.cfg";
280
281 QFile f (path);
282
283 if (!f.open (QIODevice::ReadOnly))
284 {
285 critical (fmt (QObject::tr ("Failed to open primitive categories: %1"), f.errorString()));
286 return;
287 }
288
289 PrimitiveCategory* cat = null;
290
291 while (f.atEnd() == false)
292 {
293 QString line = f.readLine();
294 int colon;
295
296 if (line.endsWith ("\n"))
297 line.chop (1);
298
299 if (line.length() == 0 || line[0] == '#')
300 continue;
301
302 if ((colon = line.indexOf (":")) == -1)
303 {
304 if (cat && cat->isValidToInclude())
305 g_PrimitiveCategories << cat;
306
307 cat = new PrimitiveCategory (line);
308 }
309 elif (cat != null)
310 {
311 QString cmd = line.left (colon);
312 ERegexType type = EFilenameRegex;
313
314 if (cmd == "f")
315 type = EFilenameRegex;
316 elif (cmd == "t")
317 type = ETitleRegex;
318 else
319 {
320 log (tr ("Warning: unknown command \"%1\" on line \"%2\""), cmd, line);
321 continue;
322 }
323
324 QRegExp regex (line.mid (colon + 1));
325 RegexEntry entry = { regex, type };
326 cat->regexes << entry;
327 }
328 else
329 log ("Warning: Rules given before the first category name");
330 }
331
332 if (cat->isValidToInclude())
333 g_PrimitiveCategories << cat;
334
335 // Add a category for unmatched primitives.
336 // Note: if this function is called the second time, g_unmatched has been
337 // deleted at the beginning of the function and is dangling at this point.
338 g_unmatched = new PrimitiveCategory (tr ("Other"));
339 g_PrimitiveCategories << g_unmatched;
340 f.close();
341 }
342
343 // =============================================================================
344 // -----------------------------------------------------------------------------
345 bool PrimitiveCategory::isValidToInclude()
346 {
347 if (regexes.size() == 0)
348 {
349 log (tr ("Warning: category \"%1\" left without patterns"), getName());
350 deleteLater();
351 return false;
352 }
353
354 return true;
355 }
356
357 // =============================================================================
358 // -----------------------------------------------------------------------------
359 bool isPrimitiveLoaderBusy()
360 {
361 return g_activeScanner != null;
362 }
363
364 // =============================================================================
365 // -----------------------------------------------------------------------------
366 static double radialPoint (int i, int divs, double (*func) (double))
367 {
368 return (*func) ((i * 2 * pi) / divs);
369 }
370
371 // =============================================================================
372 // -----------------------------------------------------------------------------
373 void makeCircle (int segs, int divs, double radius, QList<QLineF>& lines)
374 {
375 for (int i = 0; i < segs; ++i)
376 {
377 double x0 = radius * radialPoint (i, divs, cos),
378 x1 = radius * radialPoint (i + 1, divs, cos),
379 z0 = radius * radialPoint (i, divs, sin),
380 z1 = radius * radialPoint (i + 1, divs, sin);
381
382 lines << QLineF (QPointF (x0, z0), QPointF (x1, z1));
383 }
384 }
385
386 // =============================================================================
387 // -----------------------------------------------------------------------------
388 LDObjectList makePrimitive (PrimitiveType type, int segs, int divs, int num)
389 {
390 LDObjectList objs;
391 QList<int> condLineSegs;
392 QList<QLineF> circle;
393
394 makeCircle (segs, divs, 1, circle);
395
396 for (int i = 0; i < segs; ++i)
397 {
398 double x0 = circle[i].x1(),
399 x1 = circle[i].x2(),
400 z0 = circle[i].y1(),
401 z1 = circle[i].y2();
402
403 switch (type)
404 {
405 case Circle:
406 {
407 Vertex v0 (x0, 0.0f, z0),
408 v1 (x1, 0.0f, z1);
409
410 LDLine* line = new LDLine;
411 line->setVertex (0, v0);
412 line->setVertex (1, v1);
413 line->setColor (edgecolor);
414 objs << line;
415 } break;
416
417 case Cylinder:
418 case Ring:
419 case Cone:
420 {
421 double x2, x3, z2, z3;
422 double y0, y1, y2, y3;
423
424 if (type == Cylinder)
425 {
426 x2 = x1;
427 x3 = x0;
428 z2 = z1;
429 z3 = z0;
430
431 y0 = y1 = 0.0f;
432 y2 = y3 = 1.0f;
433 }
434 else
435 {
436 x2 = x1 * (num + 1);
437 x3 = x0 * (num + 1);
438 z2 = z1 * (num + 1);
439 z3 = z0 * (num + 1);
440
441 x0 *= num;
442 x1 *= num;
443 z0 *= num;
444 z1 *= num;
445
446 if (type == Ring)
447 y0 = y1 = y2 = y3 = 0.0f;
448 else
449 {
450 y0 = y1 = 1.0f;
451 y2 = y3 = 0.0f;
452 }
453 }
454
455 Vertex v0 (x0, y0, z0),
456 v1 (x1, y1, z1),
457 v2 (x2, y2, z2),
458 v3 (x3, y3, z3);
459
460 LDQuad* quad = new LDQuad;
461 quad->setColor (maincolor);
462 quad->setVertex (0, v0);
463 quad->setVertex (1, v1);
464 quad->setVertex (2, v2);
465 quad->setVertex (3, v3);
466
467 if (type == Cylinder)
468 quad->invert();
469
470 objs << quad;
471
472 if (type == Cylinder || type == Cone)
473 condLineSegs << i;
474 } break;
475
476 case Disc:
477 case DiscNeg:
478 {
479 double x2, z2;
480
481 if (type == Disc)
482 x2 = z2 = 0.0f;
483 else
484 {
485 x2 = (x0 >= 0.0f) ? 1.0f : -1.0f;
486 z2 = (z0 >= 0.0f) ? 1.0f : -1.0f;
487 }
488
489 Vertex v0 (x0, 0.0f, z0),
490 v1 (x1, 0.0f, z1),
491 v2 (x2, 0.0f, z2);
492
493 // Disc negatives need to go the other way around, otherwise
494 // they'll end up upside-down.
495 LDTriangle* seg = new LDTriangle;
496 seg->setColor (maincolor);
497 seg->setVertex (type == Disc ? 0 : 2, v0);
498 seg->setVertex (1, v1);
499 seg->setVertex (type == Disc ? 2 : 0, v2);
500 objs << seg;
501 } break;
502 }
503 }
504
505 // If this is not a full circle, we need a conditional line at the other
506 // end, too.
507 if (segs < divs && condLineSegs.size() != 0)
508 condLineSegs << segs;
509
510 for (int i : condLineSegs)
511 {
512 Vertex v0 (radialPoint (i, divs, cos), 0.0f, radialPoint (i, divs, sin)),
513 v1,
514 v2 (radialPoint (i + 1, divs, cos), 0.0f, radialPoint (i + 1, divs, sin)),
515 v3 (radialPoint (i - 1, divs, cos), 0.0f, radialPoint (i - 1, divs, sin));
516
517 if (type == Cylinder)
518 v1 = Vertex (v0[X], 1.0f, v0[Z]);
519 elif (type == Cone)
520 {
521 v1 = Vertex (v0[X] * (num + 1), 0.0f, v0[Z] * (num + 1));
522 v0[X] *= num;
523 v0[Y] = 1.0f;
524 v0[Z] *= num;
525 }
526
527 LDCondLine* line = new LDCondLine;
528 line->setColor (edgecolor);
529 line->setVertex (0, v0);
530 line->setVertex (1, v1);
531 line->setVertex (2, v2);
532 line->setVertex (3, v3);
533 objs << line;
534 }
535
536 return objs;
537 }
538
539 // =============================================================================
540 // -----------------------------------------------------------------------------
541 static QString primitiveTypeName (PrimitiveType type)
542 {
543 // Not translated as primitives are in English.
544 return type == Circle ? "Circle" :
545 type == Cylinder ? "Cylinder" :
546 type == Disc ? "Disc" :
547 type == DiscNeg ? "Disc Negative" :
548 type == Ring ? "Ring" : "Cone";
549 }
550
551 // =============================================================================
552 // -----------------------------------------------------------------------------
553 QString radialFileName (PrimitiveType type, int segs, int divs, int num)
554 {
555 int numer = segs,
556 denom = divs;
557
558 // Simplify the fractional part, but the denominator must be at least 4.
559 simplify (numer, denom);
560
561 if (denom < 4)
562 {
563 const int factor = 4 / denom;
564 numer *= factor;
565 denom *= factor;
566 }
567
568 // Compose some general information: prefix, fraction, root, ring number
569 QString prefix = (divs == lores) ? "" : fmt ("%1/", divs);
570 QString frac = fmt ("%1-%2", numer, denom);
571 QString root = g_radialNameRoots[type];
572 QString numstr = (type == Ring || type == Cone) ? fmt ("%1", num) : "";
573
574 // Truncate the root if necessary (7-16rin4.dat for instance).
575 // However, always keep the root at least 2 characters.
576 int extra = (frac.length() + numstr.length() + root.length()) - 8;
577 root.chop (clamp (extra, 0, 2));
578
579 // Stick them all together and return the result.
580 return prefix + frac + root + numstr + ".dat";
581 }
582
583 // =============================================================================
584 // -----------------------------------------------------------------------------
585 LDDocument* generatePrimitive (PrimitiveType type, int segs, int divs, int num)
586 {
587 // Make the description
588 QString frac = QString::number ((float) segs / divs);
589 QString name = radialFileName (type, segs, divs, num);
590 QString descr;
591
592 // Ensure that there's decimals, even if they're 0.
593 if (frac.indexOf (".") == -1)
594 frac += ".0";
595
596 if (type == Ring || type == Cone)
597 {
598 QString spacing =
599 (num < 10) ? " " :
600 (num < 100) ? " " : "";
601
602 descr = fmt ("%1 %2%3 x %4", primitiveTypeName (type), spacing, num, frac);
603 }
604 else
605 descr = fmt ("%1 %2", primitiveTypeName (type), frac);
606
607 // Prepend "Hi-Res" if 48/ primitive.
608 if (divs == hires)
609 descr.insert (0, "Hi-Res ");
610
611 LDDocument* f = new LDDocument;
612 f->setDefaultName (name);
613
614 QString author = APPNAME;
615 QString license = "";
616
617 if (ld_defaultname.isEmpty() == false)
618 {
619 license = getLicenseText (ld_defaultlicense);
620 author = fmt ("%1 [%2]", ld_defaultname, ld_defaultuser);
621 }
622
623 f->addObjects (
624 {
625 new LDComment (descr),
626 new LDComment (fmt ("Name: %1", name)),
627 new LDComment (fmt ("Author: %1", author)),
628 new LDComment (fmt ("!LDRAW_ORG Unofficial_%1Primitive", divs == hires ? "48_" : "")),
629 new LDComment (license),
630 new LDEmpty,
631 new LDBFC (LDBFC::CertifyCCW),
632 new LDEmpty,
633 });
634
635 f->addObjects (makePrimitive (type, segs, divs, num));
636 return f;
637 }
638
639 // =============================================================================
640 // -----------------------------------------------------------------------------
641 LDDocument* getPrimitive (PrimitiveType type, int segs, int divs, int num)
642 {
643 QString name = radialFileName (type, segs, divs, num);
644 LDDocument* f = getDocument (name);
645
646 if (f != null)
647 return f;
648
649 return generatePrimitive (type, segs, divs, num);
650 }
651
652 // =============================================================================
653 // -----------------------------------------------------------------------------
654 PrimitivePrompt::PrimitivePrompt (QWidget* parent, Qt::WindowFlags f) :
655 QDialog (parent, f)
656 {
657 ui = new Ui_MakePrimUI;
658 ui->setupUi (this);
659 connect (ui->cb_hires, SIGNAL (toggled (bool)), this, SLOT (hiResToggled (bool)));
660 }
661
662 // =============================================================================
663 // -----------------------------------------------------------------------------
664 PrimitivePrompt::~PrimitivePrompt()
665 {
666 delete ui;
667 }
668
669 // =============================================================================
670 // -----------------------------------------------------------------------------
671 void PrimitivePrompt::hiResToggled (bool on)
672 {
673 ui->sb_segs->setMaximum (on ? hires : lores);
674
675 // If the current value is 16 and we switch to hi-res, default the
676 // spinbox to 48.
677 if (on && ui->sb_segs->value() == lores)
678 ui->sb_segs->setValue (hires);
679 }
680
681 // =============================================================================
682 // -----------------------------------------------------------------------------
683 DEFINE_ACTION (MakePrimitive, 0)
684 {
685 PrimitivePrompt* dlg = new PrimitivePrompt (g_win);
686
687 if (!dlg->exec())
688 return;
689
690 int segs = dlg->ui->sb_segs->value();
691 int divs = dlg->ui->cb_hires->isChecked() ? hires : lores;
692 int num = dlg->ui->sb_ringnum->value();
693 PrimitiveType type =
694 dlg->ui->rb_circle->isChecked() ? Circle :
695 dlg->ui->rb_cylinder->isChecked() ? Cylinder :
696 dlg->ui->rb_disc->isChecked() ? Disc :
697 dlg->ui->rb_ndisc->isChecked() ? DiscNeg :
698 dlg->ui->rb_ring->isChecked() ? Ring : Cone;
699
700 LDDocument* f = generatePrimitive (type, segs, divs, num);
701
702 g_win->save (f, false);
703 delete f;
704 }

mercurial