src/primitives.cpp

changeset 310
c62edce5668c
parent 306
fef00a6cbff0
child 311
c6e38e5e4f33
equal deleted inserted replaced
309:11ec6aa1f1fb 310:c62edce5668c
17 */ 17 */
18 18
19 #include <QDir> 19 #include <QDir>
20 #include <QThread> 20 #include <QThread>
21 #include <QRegExp> 21 #include <QRegExp>
22 #include <QFileDialog>
22 #include "file.h" 23 #include "file.h"
23 #include "gui.h" 24 #include "gui.h"
24 #include "primitives.h" 25 #include "primitives.h"
26 #include "ui_makeprim.h"
25 27
26 vector<PrimitiveCategory> g_PrimitiveCategories; 28 vector<PrimitiveCategory> g_PrimitiveCategories;
27 static PrimitiveLister* g_activePrimLister = null; 29 static PrimitiveLister* g_activePrimLister = null;
28 static bool g_primListerMutex = false; 30 static bool g_primListerMutex = false;
29 vector<Primitive> g_primitives; 31 vector<Primitive> g_primitives;
267 PrimitiveCategory cat; 269 PrimitiveCategory cat;
268 cat.setName ("Other"); 270 cat.setName ("Other");
269 g_PrimitiveCategories << cat; 271 g_PrimitiveCategories << cat;
270 } 272 }
271 273
272 bool primitiveLoaderBusy() { 274 // =============================================================================
275 bool primitiveLoaderBusy()
276 {
273 return g_primListerMutex; 277 return g_primListerMutex;
274 } 278 }
279
280 vector<LDObject*> makePrimitive( PrimitiveType type, int segs, int divs, int num )
281 {
282 vector<LDObject*> objs;
283
284 for( int i = 0; i < segs; ++i )
285 {
286 double x0 = cos(( i * 2 * pi ) / divs ),
287 x1 = cos((( i + 1 ) * 2 * pi) / divs ),
288 z0 = sin(( i * 2 * pi ) / divs ),
289 z1 = sin((( i + 1 ) * 2 * pi ) / divs );
290
291 LDObject* obj = null;
292
293 switch( type )
294 {
295 case Circle:
296 {
297 vertex v0( x0, 0.0f, z0 ),
298 v1( x1, 0.0f, z1 );
299
300 LDLine* line = new LDLine;
301 line->setVertex( 0, v0 );
302 line->setVertex( 1, v1 );
303 line->setColor( edgecolor );
304 obj = line;
305 }
306 break;
307
308 case Cylinder:
309 case Ring:
310 case Cone:
311 {
312 double x2, x3, z2, z3;
313 double y0, y1, y2, y3;
314
315 if( type == Cylinder )
316 {
317 x2 = x1;
318 x3 = x0;
319 z2 = z1;
320 z3 = z0;
321
322 y0 = y1 = 0.0f;
323 y2 = y3 = 1.0f;
324 } else {
325 x2 = x1 * (num + 1);
326 x3 = x0 * (num + 1);
327 z2 = z1 * (num + 1);
328 z3 = z0 * (num + 1);
329
330 x0 *= num;
331 x1 *= num;
332 z0 *= num;
333 z1 *= num;
334
335 if( type == Ring )
336 y0 = y1 = y2 = y3 = 0.0f;
337 else
338 {
339 y0 = y1 = 1.0f;
340 y2 = y3 = 0.0f;
341 }
342 }
343
344 vertex v0( x0, y0, z0 ),
345 v1( x1, y1, z1 ),
346 v2( x2, y2, z2 ),
347 v3( x3, y3, z3 );
348
349 LDQuad* quad = new LDQuad;
350 quad->setColor( maincolor );
351 quad->setVertex( 0, v0 );
352 quad->setVertex( 1, v1 );
353 quad->setVertex( 2, v2 );
354 quad->setVertex( 3, v3 );
355 obj = quad;
356 }
357 break;
358
359 case Disc:
360 case DiscNeg:
361 {
362 double x2, z2;
363
364 if( type == Disc )
365 x2 = z2 = 0.0f;
366 else
367 {
368 x2 = ( x0 >= 0.0f ) ? 1.0f : -1.0f;
369 z2 = ( z0 >= 0.0f ) ? 1.0f : -1.0f;
370 }
371
372 vertex v0( x0, 0.0f, z0 ),
373 v1( x1, 0.0f, z1 ),
374 v2( x2, 0.0f, z2 );
375
376 LDTriangle* seg = new LDTriangle;
377 seg->setColor( maincolor );
378 seg->setVertex( 0, v0 );
379 seg->setVertex( 1, v1 );
380 seg->setVertex( 2, v2 );
381 obj = seg;
382 }
383 break;
384
385 default:
386 break;
387 }
388
389 if( obj )
390 objs << obj;
391 }
392
393 return objs;
394 }
395
396 str primitiveTypeName( PrimitiveType type )
397 {
398 return type == Circle ? "Circle" :
399 type == Cylinder ? "Cylinder" :
400 type == Disc ? "Disc" :
401 type == DiscNeg ? "Disc Negative" :
402 type == Ring ? "Ring" :
403 "Cone";
404 }
405
406 // =============================================================================
407 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
408 // =============================================================================
409 void generatePrimitive()
410 {
411 QDialog* dlg = new QDialog( g_win );
412 Ui::MakePrimUI ui;
413 ui.setupUi( dlg );
414
415 exec:
416 if( !dlg->exec() )
417 return;
418
419 int segs = ui.sb_segs->value();
420 int divs = ui.cb_hires->isChecked() ? hires : lores;
421 int num = ui.sb_ringnum->value();
422 PrimitiveType type =
423 ui.rb_circle->isChecked() ? Circle :
424 ui.rb_cylinder->isChecked() ? Cylinder :
425 ui.rb_disc->isChecked() ? Disc :
426 ui.rb_ndisc->isChecked() ? DiscNeg :
427 ui.rb_ring->isChecked() ? Ring :
428 Cone;
429
430 // Make the description
431 str descr = fmt ("%1 / %2 %3", segs, divs, primitiveTypeName( type ));
432
433 if (type == Ring || type == Cone)
434 descr += fmt (" %1", num);
435
436 LDOpenFile* f = new LDOpenFile;
437
438 *f << new LDComment( descr );
439 *f << new LDComment( fmt( "Name: ???.dat" ));
440 *f << new LDComment( fmt( "Author: LDForge" ));
441 *f << new LDComment( fmt( "!LDRAW_ORG Unofficial_%1Primitive", divs == hires ? "48_" : "" ));
442 *f << new LDComment( "Redistributable under CCAL version 2.0 : see CAreadme.txt" );
443 *f << new LDEmpty;
444 *f << new LDBFC( LDBFC::CertifyCCW );
445 *f << new LDEmpty;
446 *f << makePrimitive( type, segs, divs, num );
447
448 g_win->save( f, true );
449 delete f;
450 }

mercurial