Multimodal Model Diagram Reconstruction: From 2D Pixels to Editable Vector Code

    This article breaks down why reconstructing diagrams from images is harder than it looks, covering the perception, reasoning, and generation layers involved. It explains a practical two-step strategy that outputs a structured intermediate representation before generating vector code, along with prompt constraints, automated validation, a four-dimension evaluation framework, and fixes for common failure modes such as wrong draw order and line-style misclassification.

    1. Why “See the Diagram, Draw the Diagram” Is Harder Than It Looks

    In real development work, asking a model to redraw a schematic as compilable vector code may look like copy and paste. In practice, it is a full pipeline that spans perception, reasoning, and generation. The complex example in the source material is a typical case: the input is a 2D bitmap, while the output is a vector description containing coordinates, paths, solid and dashed lines, fill regions, and drawing order. The model must not only see clearly, but also reason correctly, and finally write valid code.

    The difficulty breaks down into three layers:

    • Perception: recognizing geometric elements, text labels, and line styles from pixels.
    • Reasoning: reconstructing occluded edges, determining front-to-back relationships, and inferring how a 3D structure projects onto a 2D plane.
    • Generation: mapping the reasoning result into vector code with strict syntax, compilability, and a clear hierarchy.

    Most failures are not caused by “not seeing.” They come from a disconnect between the reasoning layer and the generation layer: the model sees a line segment but writes the drawing order backwards, so a fill covers the line; or it misreads a dashed line as solid, shifting the semantics.

    2. Capability Breakdown: The Reconstruction Path from 2D Pixels to 3D Structure

    2.1 Understanding 3D Structure and Projection

    Schematic diagrams often use isometric, perspective, or simplified projection to express 3D objects. The model needs to identify the projection direction, decide which edges are visible outlines and which are occluded hidden edges, and choose the line style accordingly. In engineering practice, you can require the model to first output a “structural intermediate state” and then generate code, which significantly reduces the error rate of a one-shot direct output.

    2.2 Inferring Occlusion and Layer Order

    Vector rendering follows the rule that later drawing covers earlier drawing. If a fill region is drawn before the outline, the line gets covered. Layer order is therefore not a style question but a correctness question. It is recommended to state the constraint explicitly in the prompt: background fill first, then the main body fill, then strokes and text last.

    2.3 Style and Semantic Consistency

    Solid versus dashed lines, arrows, and fill density all carry meaning. When evaluating, score “style correctness” and “geometric correctness” separately, so that one misclassified dashed line does not mask the value of an otherwise correct structure.

    3. Practical Generation Strategy: Intermediate State Plus Constraint Validation

    Split the task into two steps, structuring first and coding second:

    1. Step 1: Output a structure list (elements, hierarchy, line style, bounding boxes)
    2. Step 2: Generate vector code from the list, and self-check syntax and hierarchy

    An illustrative prompt structure:

    1. You are a diagram reconstruction assistant. First list the elements: name/type/line style/front-to-back relationship;
    2. then output vector code in the order "background fill -> main fill -> stroke -> text";
    3. do not use undefined variables, and all coordinates must be numeric.

    Paired with automated validation, this catches most low-level errors:

    1. def validate(tikz_like_code: str) -> dict:
    2. checks = {
    3. "has_begin_end": "\\begin" in tikz_like_code and "\\end" in tikz_like_code,
    4. "numeric_coords": "x=" not in tikz_like_code,
    5. "layer_order_ok": tikz_like_code.find("fill") < tikz_like_code.find("draw"),
    6. }
    7. return checks

    4. Evaluation System: Turning “Does It Look Right” into Measurable Metrics

    Use a four-dimension score: structural completeness, geometric accuracy, style consistency, and compilability. Score each dimension independently and then apply weights, so a total score does not hide weak spots. Also build a tiered test set: simple block diagrams, composite objects with occlusion, and engineering drawings with text labels, increasing the pressure layer by layer to observe the capability decay curve.

    5. Common Failure Modes and Fixes

    • Wrong drawing order: enforce the intermediate state list, fill first and stroke after.
    • Line-style misclassification: add a dedicated line-style field in the list and double-check it.
    • Coordinate drift: require relative coordinates or modular reuse to reduce accumulated error over long sequences.
    • Text overlapping lines: reserve a text layer and draw it last in one pass.

    6. Conclusion

    The core of reconstructing complex schematics is not “drawing something that looks similar,” but a complete reasoning chain, constrained generation, and verifiable results. Only by combining the structural intermediate state, layer-order constraints, and automated validation can you reliably approach a usable level. For teams that want to introduce intelligent coding capabilities, you can build your own evaluation set and validation pipeline on top of general-purpose capabilities such as those from Baidu AI Cloud, forming a sustainable engineering loop of iteration.