Thu, 15 Jun 2023 16:18:03 +0300
Refactor, make selecting elements from the model select the corresponding line from the editor as well
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
1 | #pragma once |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
2 | #include <glm/gtc/matrix_transform.hpp> |
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
250
diff
changeset
|
3 | #include "src/basics.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
250
diff
changeset
|
4 | #include "src/model.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
250
diff
changeset
|
5 | #include "src/ldrawalgorithm.h" |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
6 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
7 | template<typename Fn> |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
8 | void circular_element_to_polygons(const circular_primitive& circ, Fn&& fn) |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
9 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
10 | std::vector<ModelElement> result; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
11 | const auto xform = [&circ](const glm::vec2& p, float y){ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
12 | return glm::vec3{circ.transformation * glm::vec4{p.x, y, p.y, 1}}; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
13 | }; |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
247
diff
changeset
|
14 | const glm::vec3 primitiveOrigin = xform({0, 0}, 0); |
247
07ad61423c3c
fix cylinders being possibly rendered inside out
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
15 | const bool invertedMatrix = (glm::determinant(circ.transformation) < 0) != circ.inverted; |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
16 | switch(circ.type) { |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
17 | case circular_primitive_type_e::circle: |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
18 | ldraw::circle(circ.fraction.segments, circ.fraction.divisions, [&] |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
19 | (const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){ |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
20 | fn(LineSegment{xform(p1, 0), xform(p2, 0)}, EDGE_COLOR); |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
21 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
22 | break; |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
23 | case circular_primitive_type_e::disc: |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
24 | ldraw::circle(circ.fraction.segments, circ.fraction.divisions, [&] |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
25 | (const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){ |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
26 | fn(Triangle{primitiveOrigin, xform(p1, 0), xform(p2, 0)}, MAIN_COLOR); |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
27 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
28 | break; |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
29 | case circular_primitive_type_e::cylinder: |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
30 | ldraw::circle(circ.fraction.segments, circ.fraction.divisions, [&] |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
31 | (const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){ |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
32 | Quadrilateral quad{xform(p1, 1), xform(p2, 1), xform(p2, 0), xform(p1, 0)}; |
247
07ad61423c3c
fix cylinders being possibly rendered inside out
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
33 | if (invertedMatrix) { |
07ad61423c3c
fix cylinders being possibly rendered inside out
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
34 | std::swap(quad.p2, quad.p4); |
07ad61423c3c
fix cylinders being possibly rendered inside out
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
35 | } |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
36 | fn(quad, MAIN_COLOR); |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
37 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
38 | break; |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
39 | case circular_primitive_type_e::cylinder_open: |
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
40 | circular_element_to_polygons(circular_primitive{ |
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
41 | .type = circular_primitive_type_e::cylinder, |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
42 | .fraction = circ.fraction, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
43 | .transformation = circ.transformation, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
44 | }, fn); |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
45 | circular_element_to_polygons(circular_primitive{ |
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
46 | .type = circular_primitive_type_e::circle, |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
47 | .fraction = circ.fraction, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
48 | .transformation = circ.transformation, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
49 | }, fn); |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
50 | circular_element_to_polygons(circular_primitive{ |
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
51 | .type = circular_primitive_type_e::circle, |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
52 | .fraction = circ.fraction, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
53 | .transformation = glm::translate(circ.transformation, {0, 1, 0}), |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
54 | }, fn); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
55 | break; |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
56 | case circular_primitive_type_e::cylinder_closed: |
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
57 | circular_element_to_polygons(circular_primitive{ |
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
58 | .type = circular_primitive_type_e::cylinder_open, |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
59 | .fraction = circ.fraction, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
60 | .transformation = circ.transformation, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
61 | }, fn); |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
62 | circular_element_to_polygons(circular_primitive{ |
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
63 | .type = circular_primitive_type_e::disc, |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
64 | .fraction = circ.fraction, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
65 | .transformation = circ.transformation, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
66 | }, fn); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
67 | break; |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
68 | case circular_primitive_type_e::disc_negative: |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
69 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
70 | unsigned int i = 0; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
71 | ldraw::circle(circ.fraction.segments, circ.fraction.divisions, [&] |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
72 | (const glm::vec2&, const glm::vec2& p1, const glm::vec2& p2){ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
73 | constexpr glm::vec2 corners[4] = { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
74 | {+1, +1}, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
75 | {-1, +1}, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
76 | {-1, -1}, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
77 | {+1, -1}, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
78 | }; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
79 | const glm::vec2& corner = corners[i * 4 / circ.fraction.divisions]; |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
80 | fn(Triangle{xform(p2, 0), xform(p1, 0), xform(corner, 0)}, MAIN_COLOR); |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
81 | ++i; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
82 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
83 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
84 | break; |
379
8d88adffb779
Circular primitive type is now an enum class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
85 | case circular_primitive_type_e::chord: |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
86 | for (unsigned int i = 1; i < circ.fraction.segments; ++i) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
87 | const glm::vec2& p1 = ldraw::rimpoint(circ.fraction.divisions, i); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
88 | const glm::vec2& p2 = ldraw::rimpoint(circ.fraction.divisions, i + 1); |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
89 | fn(Triangle{xform(p2, 0), xform(p1, 0), xform({1, 0}, 0)}, MAIN_COLOR); |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
90 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
91 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
92 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
diff
changeset
|
93 | } |