Debian Codegraph @

avogadro/rendering/geometryvisitor.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "geometryvisitor.h"
7
8 #include "ambientocclusionspheregeometry.h"
9 #include "curvegeometry.h"
10 #include "linestripgeometry.h"
11 #include "spheregeometry.h"
12 #include "widelinegeometry.h"
13
14 namespace Avogadro::Rendering {
15
16 GeometryVisitor::GeometryVisitor()
17 : m_center(Vector3f::Zero()), m_radius(0.0f), m_dirty(false)
18 {
19 }
20
21 GeometryVisitor::~GeometryVisitor() {}
22
23 void GeometryVisitor::visit(Drawable&) {}
24
25 void GeometryVisitor::visit(SphereGeometry& geometry)
26 {
27 const Core::Array<SphereColor>& spheres = geometry.spheres();
28 if (!spheres.size())
29 return;
30
31 m_dirty = true;
32
33 Vector3f tmpCenter(Vector3f::Zero());
34 // First find the center of the sphere geometry.
35 auto it = spheres.begin();
36 for (; it != spheres.end(); ++it)
37 tmpCenter += it->center;
38 tmpCenter /= static_cast<float>(spheres.size());
39
40 // Now find its radius.
41 float tmpRadius(0.0f);
42 if (spheres.size() > 1) {
43 for (it = spheres.begin(); it != spheres.end(); ++it) {
44 float distance = (it->center - tmpCenter).squaredNorm();
45 if (distance > tmpRadius)
46 tmpRadius = distance;
47 }
48 }
49 tmpRadius = std::sqrt(tmpRadius);
50 m_centers.push_back(tmpCenter);
51 m_radii.push_back(tmpRadius);
52 }
53
54 void GeometryVisitor::visit(AmbientOcclusionSphereGeometry& geometry)
55 {
56 const Core::Array<SphereColor>& spheres = geometry.spheres();
57 if (!spheres.size())
58 return;
59
60 m_dirty = true;
61
62 Vector3f tmpCenter(Vector3f::Zero());
63 // First find the center of the sphere geometry.
64 auto it = spheres.begin();
65 for (; it != spheres.end(); ++it)
66 tmpCenter += it->center;
67 tmpCenter /= static_cast<float>(spheres.size());
68
69 // Now find its radius.
70 float tmpRadius(0.0f);
71 if (spheres.size() > 1) {
72 for (it = spheres.begin(); it != spheres.end(); ++it) {
73 float distance = (it->center - tmpCenter).squaredNorm();
74 if (distance > tmpRadius)
75 tmpRadius = distance;
76 }
77 }
78 tmpRadius = std::sqrt(tmpRadius);
79 m_centers.push_back(tmpCenter);
80 m_radii.push_back(tmpRadius);
81 }
82
83 void GeometryVisitor::visit(CurveGeometry& cg)
84 {
85 const auto& lines = cg.lines();
86 if (lines.size() == 0) {
87 return;
88 }
89 m_dirty = true;
90 float qtty = 0.0f;
91 Vector3f tmpCenter(Vector3f::Zero());
92 for (const auto& line : lines) {
93 for (const auto& point : line->points) {
94 tmpCenter += point->pos;
95 }
96 qtty += line->points.size();
97 }
98 tmpCenter /= qtty;
99
100 float tmpRadius = 0.0f;
101 for (const auto& line : lines) {
102 for (const auto& point : line->points) {
103 float distance = (point->pos - tmpCenter).squaredNorm();
104 if (distance > tmpRadius)
105 tmpRadius = distance;
106 }
107 }
108 m_centers.push_back(tmpCenter);
109 m_radii.push_back(std::sqrt(tmpRadius));
110 }
111
112 void GeometryVisitor::visit(LineStripGeometry& lsg)
113 {
114 typedef Core::Array<LineStripGeometry::PackedVertex> VertexArray;
115 const VertexArray verts(lsg.vertices());
116 if (!verts.size())
117 return;
118
119 m_dirty = true;
120
121 Vector3f tmpCenter(Vector3f::Zero());
122 for (const auto& vert : verts) {
123 tmpCenter += vert.vertex;
124 }
125 tmpCenter /= static_cast<float>(verts.size());
126
127 float tmpRadius(0.f);
128 for (const auto& vert : verts) {
129 float distance = (vert.vertex - tmpCenter).squaredNorm();
130 if (distance > tmpRadius)
131 tmpRadius = distance;
132 }
133
134 m_centers.push_back(tmpCenter);
135 m_radii.push_back(std::sqrt(tmpRadius));
136 }
137
138 void GeometryVisitor::visit(WideLineGeometry& wlg)
139 {
140 size_t numLines = wlg.lineCount();
141 if (numLines == 0)
142 return;
143
144 m_dirty = true;
145
146 // Each line segment has 4 vertices (2 endpoints x 2 sides).
147 // Only sample unique endpoints: indices 0 and 2 per quad.
148 const auto& verts = wlg.vertices();
149 Vector3f tmpCenter(Vector3f::Zero());
150 for (size_t i = 0; i < verts.size(); i += 4) {
151 tmpCenter += verts[i].position;
152 tmpCenter += verts[i + 2].position;
153 }
154 tmpCenter /= static_cast<float>(numLines * 2);
155
156 float tmpRadius(0.f);
157 for (size_t i = 0; i < verts.size(); i += 4) {
158 float d1 = (verts[i].position - tmpCenter).squaredNorm();
159 float d2 = (verts[i + 2].position - tmpCenter).squaredNorm();
160 if (d1 > tmpRadius)
161 tmpRadius = d1;
162 if (d2 > tmpRadius)
163 tmpRadius = d2;
164 }
165
166 m_centers.push_back(tmpCenter);
167 m_radii.push_back(std::sqrt(tmpRadius));
168 }
169
170 void GeometryVisitor::clear()
171 {
172 m_center = Vector3f::Zero();
173 m_radius = 0.0f;
174 m_dirty = false;
175 m_centers.clear();
176 m_radii.clear();
177 }
178
179 Vector3f GeometryVisitor::center()
180 {
181 average();
182 return m_center;
183 }
184
185 float GeometryVisitor::radius()
186 {
187 average();
188 return m_radius;
189 }
190
191 void GeometryVisitor::average()
192 {
193 if (!m_dirty)
194 return;
195
196 // Find the average position of the center, then the minimal enclosing radius.
197 m_dirty = false;
198 if (m_centers.size() == 1) {
199 m_center = m_centers[0];
200 m_radius = m_radii[0];
201 } else {
202 m_center = Vector3f::Zero();
203 std::vector<Vector3f>::const_iterator cit;
204 for (cit = m_centers.begin(); cit != m_centers.end(); ++cit)
205 m_center += *cit;
206 m_center /= static_cast<float>(m_centers.size());
207 // Now find the smallest enclosing radius for the new center.
208 m_radius = 0.0f;
209 std::vector<float>::const_iterator rit;
210 for (cit = m_centers.begin(), rit = m_radii.begin();
211 cit != m_centers.end() && rit != m_radii.end(); ++cit, ++rit) {
212 float distance = (m_center - (*cit)).norm() + (*rit);
213 if (distance > m_radius)
214 m_radius = distance;
215 }
216 }
217 }
218
219 } // namespace Avogadro::Rendering