BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
geometry.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/ba3d/model/geometry.cpp
6 //! @brief Implements Geometry class
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "Base/Util/Assert.h"
17 #include "GUI/ba3d/model/model.h"
18 
19 namespace GUI::RealSpace {
20 
21 //------------------------------------------------------------------------------
22 
24  : v(v_)
25  , n(n_)
26 {
27 }
28 
30 {
31  for (int i = 0; i < n; ++i)
32  append(v);
33 }
34 
35 void Geometry::Vertices::addTriangle(const Vector3D& v1, const Vector3D& v2, const Vector3D& v3)
36 {
37  append(v1);
38  append(v2);
39  append(v3);
40 }
41 
42 void Geometry::Vertices::addQuad(const Vector3D& v1, const Vector3D& v2, const Vector3D& v3,
43  const Vector3D& v4)
44 {
45  addTriangle(v1, v2, v3);
46  addTriangle(v3, v4, v1);
47 }
48 
49 void Geometry::Vertices::addQuad(const Vertices& vs, unsigned i1, unsigned i2, unsigned i3,
50  unsigned i4)
51 {
52  addQuad(vs.at(i1), vs.at(i2), vs.at(i3), vs.at(i4));
53 }
54 
55 void Geometry::Vertices::addStrip(const Vertices& vs, const Indices& is)
56 {
57  ASSERT(is.size() >= 3); // at least one triangle
58  for (unsigned i = 0; i + 2 < is.size(); ++i)
59  if (i % 2)
60  addTriangle(vs.at(is.at(i)), vs.at(is.at(1 + i)), vs.at(is.at(2 + i)));
61  else
62  addTriangle(vs.at(is.at(i)), vs.at(is.at(2 + i)), vs.at(is.at(1 + i)));
63 }
64 
65 void Geometry::Vertices::addFan(const Vertices& vs, const Indices& is)
66 {
67  ASSERT(is.size() >= 3); // at least one triangle
68  const auto& ctr = vs.at(is.at(0));
69  for (unsigned i = 0; i + 2 < is.size(); ++i)
70  addTriangle(ctr, vs.at(is.at(1 + i)), vs.at(is.at(2 + i)));
71 }
72 
73 //------------------------------------------------------------------------------
74 
76  : m_key(key_)
77 {
78  using namespace GeometricID;
79 
80  switch (m_key.id) {
81  case BaseShape::Plane:
82  m_mesh = meshPlane();
83  break;
84  case BaseShape::Box:
85  m_mesh = meshBox();
86  break;
87  case BaseShape::Sphere:
89  break;
90  case BaseShape::Column:
92  break;
93  case BaseShape::Icosahedron:
95  break;
96  case BaseShape::Dodecahedron:
98  break;
99  case BaseShape::TruncatedBox:
101  break;
102  case BaseShape::Bipyramid4:
104  break;
105  case BaseShape::Ripple:
107  break;
108  }
109 }
110 
112 {
113  // remove self from the store
115 }
116 
118 {
119  int nv = vs.count();
120  ASSERT(0 == nv % 3);
121  ASSERT(!ns || nv == ns->count()); // if normals not given, will be computed
122 
123  Mesh mesh(nv);
124 
125  for (int i = 0; i < nv; i += 3) {
126  const Vector3D& v0 = vs.at(0 + i);
127  const Vector3D& v1 = vs.at(1 + i);
128  const Vector3D& v2 = vs.at(2 + i);
129  const Vector3D* n0;
130  const Vector3D* n1;
131  const Vector3D* n2;
132  Vector3D nm;
133 
134  if (ns) {
135  n0 = &ns->at(0 + i);
136  n1 = &ns->at(1 + i);
137  n2 = &ns->at(2 + i);
138  } else {
139  nm = cross((v1 - v0), (v2 - v0));
140  n0 = n1 = n2 = &nm;
141  }
142 
143  mesh.append(Vert_Normal(v0, *n0));
144  mesh.append(Vert_Normal(v1, *n1));
145  mesh.append(Vert_Normal(v2, *n2));
146  }
147 
148  return mesh;
149 }
150 
152 {
153  return makeMesh(vs, &ns);
154 }
155 
156 //------------------------------------------------------------------------------
157 
159 {
160  auto it = m_geometries.find(key);
161  if (m_geometries.end() != it) {
162  if (auto g = it->second.lock())
163  return g;
164  }
165  GeometryHandle g(new Geometry(key));
166  m_geometries[key] = GeometryRef(g);
167  return g;
168 }
169 
171 {
172  emit deletingGeometry(&g);
173  m_geometries.erase(g.m_key);
174 }
175 
177 {
178  static GeometryStore gs;
179  return gs;
180 }
181 
182 //------------------------------------------------------------------------------
183 
184 } // namespace GUI::RealSpace
GeometryHandle getGeometry(GeometricID::Key)
Definition: geometry.cpp:158
void geometryDeleted(Geometry const &)
Definition: geometry.cpp:170
static Mesh meshPlane()
Definition: plane.cpp:20
static Mesh meshBox()
Definition: box.cpp:20
static Mesh makeMesh(const Vertices &vs, Vertices const *ns=nullptr)
Definition: geometry.cpp:117
GeometricID::Key m_key
Definition: geometry.h:72
QVector< Vert_Normal > Mesh
Definition: geometry.h:66
static Mesh meshColumn(float ratio_Rt_Rb, float numSides)
Definition: column.cpp:22
static Mesh meshBipyramid4(float rH, float alpha, float H)
static Mesh meshTruncBox(float tD)
Definition: truncbox.cpp:20
Geometry(GeometricID::Key)
Definition: geometry.cpp:75
static Mesh meshDodecahedron()
static Mesh meshIcosahedron()
Definition: icosahedron.cpp:22
static Mesh meshSphere(float cut, float baseShift=0.0f, float removedTop=0.0f)
Definition: sphere.cpp:24
std::vector< unsigned > Indices
Definition: geometry.h:42
static Mesh meshRipple(float numSides, float ratio_asymmetry_W)
Definition: ripple.cpp:21
Defines Geometry class.
Defines Model class.
Vector3D cross(const Vector3D &v1, const Vector3D &v2)
Definition: def.cpp:67
std::shared_ptr< Geometry > GeometryHandle
Definition: geometry_inc.h:27
GeometryStore & geometryStore()
Definition: geometry.cpp:176
std::weak_ptr< Geometry > GeometryRef
Definition: geometry_inc.h:28
Real shapes will be parameterized by BaseShape enum and possibly two floats.
Definition: geometry_inc.h:51
void addTriangle(const Vector3D &, const Vector3D &, const Vector3D &)
Definition: geometry.cpp:35
void addStrip(const Vertices &, const Indices &)
Definition: geometry.cpp:55
void addQuad(const Vector3D &, const Vector3D &, const Vector3D &, const Vector3D &)
Definition: geometry.cpp:42
void addVertex(const Vector3D &, int n=1)
Definition: geometry.cpp:29
void addFan(const Vertices &, const Indices &)
Definition: geometry.cpp:65