src/primitives.cc

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

mercurial