Tue, 16 Jul 2013 21:02:12 +0300
restyle
src/primitives.cpp | file | annotate | diff | comparison | revisions |
--- a/src/primitives.cpp Tue Jul 16 20:01:36 2013 +0300 +++ b/src/primitives.cpp Tue Jul 16 21:02:12 2013 +0300 @@ -32,7 +32,7 @@ static bool g_primListerMutex = false; vector<Primitive> g_primitives; -static const str g_Other = QObject::tr( "Other" ); +static const str g_Other = QObject::tr ("Other"); static void populateCategories(); static void loadPrimitiveCatgories(); @@ -40,32 +40,27 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -void loadPrimitives() -{ - print( "Loading primitives...\n" ); - +void loadPrimitives() { + print ("Loading primitives...\n"); loadPrimitiveCatgories(); // Try to load prims.cfg - File conf( config::dirpath() + "prims.cfg", File::Read ); + File conf (config::dirpath() + "prims.cfg", File::Read); - if( !conf ) - { + if (!conf) { // No prims.cfg, build it PrimitiveLister::start(); - } - else - { - for( str line : conf ) - { - int space = line.indexOf( " " ); + } else { + // Read primitives from prims.cfg + for (str line : conf) { + int space = line.indexOf (" "); - if( space == -1 ) + if (space == -1) continue; Primitive info; - info.name = line.left( space ); - info.title = line.mid( space + 1 ); + info.name = line.left (space); + info.title = line.mid (space + 1); g_primitives << info; } @@ -76,17 +71,15 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -void recursiveGetFilenames( QDir dir, vector<str>& fnames ) -{ +static void recursiveGetFilenames (QDir dir, vector<str>& fnames) { QFileInfoList flist = dir.entryInfoList(); - for( const QFileInfo & info : flist ) - { - if( info.fileName() == "." || info.fileName() == ".." ) + for (const QFileInfo & info : flist) { + if (info.fileName() == "." || info.fileName() == "..") continue; // skip . and .. - if( info.isDir() ) - recursiveGetFilenames( QDir( info.absoluteFilePath() ), fnames ); + if (info.isDir()) + recursiveGetFilenames (QDir (info.absoluteFilePath()), fnames); else fnames << info.absoluteFilePath(); } @@ -95,49 +88,46 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -void PrimitiveLister::work() -{ +void PrimitiveLister::work() { g_activePrimLister = this; m_prims.clear(); - QDir dir( LDPaths::prims() ); + QDir dir (LDPaths::prims()); ulong baselen = dir.absolutePath().length(); ulong i = 0; vector<str> fnames; - assert( dir.exists() ); - recursiveGetFilenames( dir, fnames ); - emit starting( fnames.size() ); + assert (dir.exists()); + recursiveGetFilenames (dir, fnames); + emit starting (fnames.size()); - for( str fname : fnames ) - { - File f( fname, File::Read ); + for (str fname : fnames) { + File f (fname, File::Read); Primitive info; - info.name = fname.mid( baselen + 1 ); // make full path relative - info.name.replace( '/', '\\' ); // use DOS backslashes, they're expected + info.name = fname.mid (baselen + 1); // make full path relative + info.name.replace ('/', '\\'); // use DOS backslashes, they're expected info.cat = null; - if( !f.readLine( info.title )) + if (!f.readLine (info.title)) info.title = ""; info.title = info.title.simplified(); - if( info.title[0] == '0' ) - { - info.title.remove( 0, 1 ); // remove 0 + if (info.title[0] == '0') { + info.title.remove (0, 1); // remove 0 info.title = info.title.simplified(); } - + m_prims << info; - emit update( ++i ); + emit update (++i); } // Save to a config file - File conf( config::dirpath() + "prims.cfg", File::Write ); + File conf (config::dirpath() + "prims.cfg", File::Write); - for( Primitive & info : m_prims ) - fprint( conf, "%1 %2\n", info.name, info.title ); + for (Primitive & info : m_prims) + fprint (conf, "%1 %2\n", info.name, info.title); conf.close(); @@ -152,26 +142,24 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -void PrimitiveLister::start() -{ - if( g_activePrimLister ) +void PrimitiveLister::start() { + if (g_activePrimLister) return; PrimitiveLister* lister = new PrimitiveLister; QThread* listerThread = new QThread; - lister->moveToThread( listerThread ); - connect( lister, SIGNAL( starting( ulong )), g_win, SLOT( primitiveLoaderStart( ulong )) ); - connect( lister, SIGNAL( update( ulong )), g_win, SLOT( primitiveLoaderUpdate( ulong )) ); - connect( lister, SIGNAL( workDone() ), g_win, SLOT( primitiveLoaderEnd() )); - connect( listerThread, SIGNAL( started() ), lister, SLOT( work() )); - connect( listerThread, SIGNAL( finished() ), lister, SLOT( deleteLater() )); + lister->moveToThread (listerThread); + connect (lister, SIGNAL (starting (ulong)), g_win, SLOT (primitiveLoaderStart (ulong))); + connect (lister, SIGNAL (update (ulong)), g_win, SLOT (primitiveLoaderUpdate (ulong))); + connect (lister, SIGNAL (workDone()), g_win, SLOT (primitiveLoaderEnd())); + connect (listerThread, SIGNAL (started()), lister, SLOT (work())); + connect (listerThread, SIGNAL (finished()), lister, SLOT (deleteLater())); listerThread->start(); } -static PrimitiveCategory* findCategory( str name ) -{ - for( PrimitiveCategory & cat : g_PrimitiveCategories ) - if( cat.name() == name ) +static PrimitiveCategory* findCategory (str name) { + for (PrimitiveCategory& cat : g_PrimitiveCategories) + if (cat.name() == name) return &cat; return null; @@ -180,59 +168,52 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -static void populateCategories() -{ - for( PrimitiveCategory & cat : g_PrimitiveCategories ) +static void populateCategories() { + for (PrimitiveCategory& cat : g_PrimitiveCategories) cat.prims.clear(); - PrimitiveCategory* unmatched = findCategory( g_Other ); + PrimitiveCategory* unmatched = findCategory (g_Other); - if( !unmatched ) - { + if (!unmatched) { // Shouldn't happen.. but catch it anyway. PrimitiveCategory cat; - cat.setName( g_Other ); - unmatched = &( g_PrimitiveCategories << cat ); + cat.setName (g_Other); + unmatched = & (g_PrimitiveCategories << cat); } - - for( Primitive & prim : g_primitives ) - { + + for (Primitive& prim : g_primitives) { bool matched = false; // Go over the categories and their regexes, if and when there's a match, // the primitive's category is set to the category the regex beloings to. - for( PrimitiveCategory & cat : g_PrimitiveCategories ) - { - for( PrimitiveCategory::RegexEntry & entry : cat.regexes ) - { - switch( entry.type ) - { + for (PrimitiveCategory& cat : g_PrimitiveCategories) { + for (PrimitiveCategory::RegexEntry& entry : cat.regexes) { + switch (entry.type) { case PrimitiveCategory::Filename: // f-regex, check against filename - matched = entry.regex.exactMatch( prim.name ); + matched = entry.regex.exactMatch (prim.name); break; case PrimitiveCategory::Title: // t-regex, check against title - matched = entry.regex.exactMatch( prim.title ); + matched = entry.regex.exactMatch (prim.title); break; } - if( matched ) - { + if (matched) { prim.cat = &cat; break; } } // Drop out if a category was decided on. - if( prim.cat ) + if (prim.cat) break; } // If there was a match, add the primitive to the category. // Otherwise, add it to the list of unmatched primitives. - if( prim.cat ) + if (prim.cat) prim.cat->prims << prim; else unmatched->prims << prim; @@ -242,101 +223,93 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -static void loadPrimitiveCatgories() -{ +static void loadPrimitiveCatgories() { g_PrimitiveCategories.clear(); - - File f( config::dirpath() + "primregexps.cfg", File::Read ); + File f (config::dirpath() + "primregexps.cfg", File::Read); - if( !f ) - f.open( ":/data/primitive-categories.cfg", File::Read ); + if (!f) + f.open (":/data/primitive-categories.cfg", File::Read); - if( !f ) - critical( QObject::tr( "Failed to open primitive categories!" )); + if (!f) + critical (QObject::tr ("Failed to open primitive categories!")); - if( f ) - { + if (f) { PrimitiveCategory cat; - for( str line : f ) - { + for (str line : f) { int colon; - if( line.length() == 0 || line[0] == '#' ) + if (line.length() == 0 || line[0] == '#') continue; - if( ( colon = line.indexOf( ":" )) == -1 ) - { - if( cat.regexes.size() > 0 ) + if ((colon = line.indexOf (":")) == -1) { + if (cat.regexes.size() > 0) g_PrimitiveCategories << cat; cat.regexes.clear(); cat.prims.clear(); - cat.setName( line ); - } - else - { - str cmd = line.left( colon ); - + cat.setName (line); + } else { + str cmd = line.left (colon); PrimitiveCategory::Type type = PrimitiveCategory::Filename; - if( cmd == "f" ) + if (cmd == "f") type = PrimitiveCategory::Filename; - elif( cmd == "t" ) + elif (cmd == "t") type = PrimitiveCategory::Title; else continue; - QRegExp regex( line.mid( colon + 1 )); + QRegExp regex (line.mid (colon + 1)); PrimitiveCategory::RegexEntry entry = { regex, type }; cat.regexes << entry; } } - if( cat.regexes.size() > 0 ) + if (cat.regexes.size() > 0) g_PrimitiveCategories << cat; } // Add a category for unmatched primitives PrimitiveCategory cat; - cat.setName( g_Other ); + cat.setName (g_Other); g_PrimitiveCategories << cat; } // ============================================================================= -bool primitiveLoaderBusy() -{ +bool primitiveLoaderBusy() { return g_primListerMutex; } -double radialPoint( int i, int divs, double ( *func )( double )) -{ - return ( *func )(( i * 2 * pi ) / divs ); +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +static double radialPoint (int i, int divs, double (*func) (double)) { + return (*func) ( (i * 2 * pi) / divs); } -vector<LDObject*> makePrimitive( PrimitiveType type, int segs, int divs, int num ) -{ +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +vector<LDObject*> makePrimitive (PrimitiveType type, int segs, int divs, int num) { vector<LDObject*> objs; vector<int> condLineSegs; - for( int i = 0; i < segs; ++i ) - { - double x0 = radialPoint( i, divs, cos ), - x1 = radialPoint( i + 1, divs, cos ), - z0 = radialPoint( i, divs, sin ), - z1 = radialPoint( i + 1, divs, sin ); + for (int i = 0; i < segs; ++i) { + double x0 = radialPoint (i, divs, cos), + x1 = radialPoint (i + 1, divs, cos), + z0 = radialPoint (i, divs, sin), + z1 = radialPoint (i + 1, divs, sin); - switch( type ) - { - case Circle: - { - vertex v0( x0, 0.0f, z0 ), - v1( x1, 0.0f, z1 ); + switch (type) { + case Circle: { + vertex v0 (x0, 0.0f, z0), + v1 (x1, 0.0f, z1); LDLineObject* line = new LDLineObject; - line->setVertex( 0, v0 ); - line->setVertex( 1, v1 ); - line->setColor( edgecolor ); + line->setVertex (0, v0); + line->setVertex (1, v1); + line->setColor (edgecolor); objs << line; } break; @@ -344,89 +317,84 @@ case Cylinder: case Ring: case Cone: - { - double x2, x3, z2, z3; - double y0, y1, y2, y3; - - if( type == Cylinder ) { - x2 = x1; - x3 = x0; - z2 = z1; - z3 = z0; - - y0 = y1 = 0.0f; - y2 = y3 = 1.0f; - } - else - { - x2 = x1 * ( num + 1 ); - x3 = x0 * ( num + 1 ); - z2 = z1 * ( num + 1 ); - z3 = z0 * ( num + 1 ); - - x0 *= num; - x1 *= num; - z0 *= num; - z1 *= num; + double x2, x3, z2, z3; + double y0, y1, y2, y3; - if( type == Ring ) - y0 = y1 = y2 = y3 = 0.0f; - else - { - y0 = y1 = 1.0f; - y2 = y3 = 0.0f; + if (type == Cylinder) { + x2 = x1; + x3 = x0; + z2 = z1; + z3 = z0; + + y0 = y1 = 0.0f; + y2 = y3 = 1.0f; + } else { + x2 = x1 * (num + 1); + x3 = x0 * (num + 1); + z2 = z1 * (num + 1); + z3 = z0 * (num + 1); + + x0 *= num; + x1 *= num; + z0 *= num; + z1 *= num; + + if (type == Ring) + y0 = y1 = y2 = y3 = 0.0f; + else { + y0 = y1 = 1.0f; + y2 = y3 = 0.0f; + } } + + vertex v0 (x0, y0, z0), + v1 (x1, y1, z1), + v2 (x2, y2, z2), + v3 (x3, y3, z3); + + LDQuadObject* quad = new LDQuadObject; + quad->setColor (maincolor); + quad->setVertex (0, v0); + quad->setVertex (1, v1); + quad->setVertex (2, v2); + quad->setVertex (3, v3); + + if (type == Cylinder) + quad->invert(); + + objs << quad; + + if (type == Cylinder || type == Cone) + condLineSegs << i; } - - vertex v0( x0, y0, z0 ), - v1( x1, y1, z1 ), - v2( x2, y2, z2 ), - v3( x3, y3, z3 ); - - LDQuadObject* quad = new LDQuadObject; - quad->setColor( maincolor ); - quad->setVertex( 0, v0 ); - quad->setVertex( 1, v1 ); - quad->setVertex( 2, v2 ); - quad->setVertex( 3, v3 ); - - if( type == Cylinder ) - quad->invert(); - - objs << quad; - - if( type == Cylinder || type == Cone ) - condLineSegs << i; - } - break; + break; case Disc: case DiscNeg: - { - double x2, z2; - - if( type == Disc ) - x2 = z2 = 0.0f; - else { - x2 = ( x0 >= 0.0f ) ? 1.0f : -1.0f; - z2 = ( z0 >= 0.0f ) ? 1.0f : -1.0f; + double x2, z2; + + if (type == Disc) + x2 = z2 = 0.0f; + else { + x2 = (x0 >= 0.0f) ? 1.0f : -1.0f; + z2 = (z0 >= 0.0f) ? 1.0f : -1.0f; + } + + vertex v0 (x0, 0.0f, z0), + v1 (x1, 0.0f, z1), + v2 (x2, 0.0f, z2); + + // Disc negatives need to go the other way around, otherwise + // they'll end up upside-down. + LDTriangleObject* seg = new LDTriangleObject; + seg->setColor (maincolor); + seg->setVertex (type == Disc ? 0 : 2, v0); + seg->setVertex (1, v1); + seg->setVertex (type == Disc ? 2 : 0, v2); + objs << seg; } - - vertex v0( x0, 0.0f, z0 ), - v1( x1, 0.0f, z1 ), - v2( x2, 0.0f, z2 ); - - // Disc negatives need to go the other way around, otherwise - // they'll end up upside-down. - LDTriangleObject* seg = new LDTriangleObject; - seg->setColor( maincolor ); - seg->setVertex( type == Disc ? 0 : 2, v0 ); - seg->setVertex( 1, v1 ); - seg->setVertex( type == Disc ? 2 : 0, v2 ); - objs << seg; - } break; default: @@ -436,50 +404,49 @@ // If this is not a full circle, we need a conditional line at the other // end, too. - if( segs < divs && condLineSegs.size() != 0 ) + if (segs < divs && condLineSegs.size() != 0) condLineSegs << segs; - for( int i : condLineSegs ) - { - vertex v0( radialPoint( i, divs, cos ), 0.0f, radialPoint( i, divs, sin )), + for (int i : condLineSegs) { + vertex v0 (radialPoint (i, divs, cos), 0.0f, radialPoint (i, divs, sin)), v1, - v2( radialPoint( i + 1, divs, cos ), 0.0f, radialPoint( i + 1, divs, sin )), - v3( radialPoint( i - 1, divs, cos ), 0.0f, radialPoint( i - 1, divs, sin )); + v2 (radialPoint (i + 1, divs, cos), 0.0f, radialPoint (i + 1, divs, sin)), + v3 (radialPoint (i - 1, divs, cos), 0.0f, radialPoint (i - 1, divs, sin)); - if( type == Cylinder ) - v1 = vertex( v0[X], 1.0f, v0[Z] ); - elif( type == Cone ) - { - v1 = vertex( v0[X] * ( num + 1 ), 0.0f, v0[Z] * ( num + 1 )); + if (type == Cylinder) + v1 = vertex (v0[X], 1.0f, v0[Z]); + elif (type == Cone) { + v1 = vertex (v0[X] * (num + 1), 0.0f, v0[Z] * (num + 1)); v0[X] *= num; v0[Y] = 1.0f; v0[Z] *= num; } LDCondLineObject* line = new LDCondLineObject; - line->setColor( edgecolor ); - line->setVertex( 0, v0 ); - line->setVertex( 1, v1 ); - line->setVertex( 2, v2 ); - line->setVertex( 3, v3 ); + line->setColor (edgecolor); + line->setVertex (0, v0); + line->setVertex (1, v1); + line->setVertex (2, v2); + line->setVertex (3, v3); objs << line; } return objs; } -str primitiveTypeName( PrimitiveType type ) -{ +// ============================================================================= +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +// ============================================================================= +static str primitiveTypeName (PrimitiveType type) { // Not translated as primitives are in English. return type == Circle ? "Circle" : - type == Cylinder ? "Cylinder" : - type == Disc ? "Disc" : - type == DiscNeg ? "Disc Negative" : - type == Ring ? "Ring" : "Cone"; + type == Cylinder ? "Cylinder" : + type == Disc ? "Disc" : + type == DiscNeg ? "Disc Negative" : + type == Ring ? "Ring" : "Cone"; } -static const str g_radialNameRoots[] = -{ +static const str g_radialNameRoots[] = { "edge", "cyli", "disc", @@ -491,16 +458,14 @@ // ============================================================================= // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // ============================================================================= -str radialFileName( PrimitiveType type, int segs, int divs, int num ) -{ +str radialFileName (PrimitiveType type, int segs, int divs, int num) { short numer = segs, - denom = divs; + denom = divs; // Simplify the fractional part, but the denominator must be at least 4. - simplify( numer, denom ); + simplify (numer, denom); - if( denom < 4 ) - { + if (denom < 4) { const short factor = 4 / denom; numer *= factor; @@ -508,15 +473,15 @@ } // Compose some general information: prefix, fraction, root, ring number - str prefix = ( divs == lores ) ? "" : fmt( "%1/", divs ); - str frac = fmt( "%1-%2", numer, denom ); + str prefix = (divs == lores) ? "" : fmt ("%1/", divs); + str frac = fmt ("%1-%2", numer, denom); str root = g_radialNameRoots[type]; - str numstr = ( type == Ring || type == Cone ) ? fmt( "%1", num ) : ""; + str numstr = (type == Ring || type == Cone) ? fmt ("%1", num) : ""; // Truncate the root if necessary (7-16rin4.dat for instance). // However, always keep the root at least 2 characters. - int extra = ( frac.length() + numstr.length() + root.length()) - 8; - root.chop( min<short>( max<short>( extra, 0 ), 2 )); + int extra = (frac.length() + numstr.length() + root.length()) - 8; + root.chop (min<short> (max<short> (extra, 0), 2)); // Stick them all together and return the result. return prefix + frac + root + numstr + ".dat"; @@ -544,7 +509,7 @@ ui.rb_ring->isChecked() ? Ring : Cone; // Make the description - str frac = ftoa (((float) segs) / divs); + str frac = ftoa ( ( (float) segs) / divs); str name = radialFileName (type, segs, divs, num); str descr; @@ -558,7 +523,7 @@ descr = fmt ("%1 %2", primitiveTypeName (type), frac); LDOpenFile* f = new LDOpenFile; - f->setName( QFileDialog::getSaveFileName( null, QObject::tr( "Save Primitive" ), name )); + f->setName (QFileDialog::getSaveFileName (null, QObject::tr ("Save Primitive"), name)); *f << new LDCommentObject (descr); *f << new LDCommentObject (fmt ("Name: %1", name)); @@ -572,4 +537,4 @@ g_win->save (f, false); delete f; -} +} \ No newline at end of file