LEGO® MINDSTORMS® and Technic®MINDSTORMS® EV3/NXTMINDSTORMS SensorsLEGO® technical dataLDrawMiscellaneous LEGO®VEX;LEGO® & PhotographyPanoramic PhotographyPhoto GalleryHome

 

 

     


    Rectifier, LDraw file optimizer

    Rectifier utility scans a LDraw file and performs two kinds of optimization on it:

    • Convert adjacent triangles into a quad. This conversion is done only if the resulting quad is convex, flat enough (less than 1° warp) and if the triangles have the same winding and color.
    • Convert rhombuses (quads that have parallel opposite sides) bordered by at least one line into a rect primitive. Rhombuses must be perfectly flat to allow this conversion.

    It is a simple console application, source code is provided below to anyone willing to integrate it in a more palatable interface. You may also use Michael Heidemann LETGUI front-end (highly recommended!).

    Download

      Rectifier package, including program for Windows, documentation, source files (Visual C++ 6.0), sample files.

    History

    • V1.0: Initial release
    • V1.1: Corrected a stack size issue that caused crash under Windows 2000.
    • V1.2: Rectifier now cares to BFC orientation when replacing quads with rect primitives. If no BFC orientation is specified in the input file, CCW is assumed. Also tries to keep simple coefficients in rect primitive transformation matrix when possible.
    • V1.3: Corrected a bug that caused wrong coefficient in small rect
    • V1.4: Added -t option allowing to choose the maximum angle formed by two triangles being merged into a quad.
    • V1.5: Added -a option allowing to prevent quad to rect conversion when the quad has adjacent condline(s)
    • V1.6: Used dynamic memory allocation to enable the use on big LDraw files.
    • V1.7: Manages direct color (code courtesy of Nils Schmidt). Prevents creation of quads with too flat angles.
    • V1.8: Corrected a bug introduced in version 1.7 (wrong file output of edge lines).
    • V1.9: Simpler matrix coefficients for generated rect primitive! The LDraw code is even tighter. The program is simpler, too ;)

    Usage

    • Prepare the input LDraw file. Rectifier works on any LDraw file, but should be used in the end of authoring process, before submitting parts to tracker.
    • Launch a command prompt
    • Type the command line: rectifier [-c] [-q] [-r] [-a] [-t <value>] LdrawFileIn LdrawFileOut. Rectifier will create optimized LdrawFileOut. Note that if file LdrawFileOut exists it will be overwritten without warning. -c, -q and -r are optional parameters controlling the behavior of Rectifier. These options will be detailed below.

    Here is a screen shot of a sample run:

    How Rectifier works

    • Input file is read and parsed. Triangles, quads and lines are stored in an array (limited by memory... or patience!).
    • A first scan is done to locate triangles of the same color that share a common side. For each candidate we check if the formed quad would be convex and planar enough. If a quad is found, it replaces the first triangle and the second is deleted. We then search for a conditional line that would have been set between the two triangles. If one is found it is suppressed.
    • We then scan again the array, this time looking for rhombuses. They are identified by their diagonals which must cross in their middle. When a rhombus is found, we look for edge lines bordering the rhombus. According to the configuration of these edge lines, we determine the type of rect primitive to use among rect1, rect2p, rect2a, rect3 and rect, as well as its orientation. Center of the rhombus will become center of primitive, while length and orientation of its sides determine the transformation matrix of the primitive. Rect primitive will be inserted in file at previous position of the rhombus, lines (incorporated in rect) are deleted.
    • Rectifier then writes output file, changing only modified or deleted lines.
    • Behaviour of Rectifier can be controlled with the following command line parameters:
      1. -c: Converted triangles are colored in yellow, newly formed rect primitives are colored blue. This helps appreciating the work of Rectifier.
      2. -q: Rectifier does not attempt to transform triangles into quads.
      3. -a: Rectifier does not convert quad to rect when the quad has adjacent condline(s). This may improve smooth shading of the part.
      4. -r: Rectifier does not convert bordered rhombuses into rect primitives.
      5. -t <value>: defines the maximum angle formed by two triangles to be merged into a quad. Default value is 0.95°, matching the LDraw recommended value.

      Example

      This file (64053.dat) was processed with Rectifier. It located several adjacent triangles (yellow) and combined some quads and lines into rect primitives. As a result, the file is shrunk by 24%...

      Command line: rectifier -c 64053.dat 64053r.dat

      LDraw.org

     

LEGO® MINDSTORMS® and Technic®MINDSTORMS® EV3/NXTMINDSTORMS SensorsLEGO® technical dataLDrawMiscellaneous LEGO®VEX;LEGO® & PhotographyPanoramic PhotographyPhoto GalleryHome