File: make-a-dodec.cc

package info (click to toggle)
coot 1.1.18%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,004 kB
  • sloc: cpp: 495,934; python: 35,043; ansic: 26,143; lisp: 22,768; sh: 13,186; makefile: 2,746; awk: 441; xml: 245; csh: 14
file content (169 lines) | stat: -rw-r--r-- 6,809 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169

// spikey mode is 0 for smooth shaded and
// 1 for spikey/flat shaded

#include <map>

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtx/string_cast.hpp>

#include "make-a-dodec.hh"
#include "utils/dodec.hh"

glm::vec3
clipper_to_glm(const clipper::Coord_orth &co) {
   return glm::vec3(co.x(), co.y(), co.z());
}

std::pair<std::vector<coot::api::vn_vertex>, std::vector<g_triangle> >
make_dodec() {

   std::pair<std::vector<coot::api::vn_vertex>, std::vector<g_triangle> > p;
   std::vector<coot::api::vn_vertex> &vertices = p.first;
   std::vector<g_triangle> &triangles = p.second;

   if (true) {
      pentakis_dodec p_dodec;
      std::vector<clipper::Coord_orth> dodec_vertices_c = p_dodec.d.coords();
      std::vector<clipper::Coord_orth> pyrimid_vertices_c = p_dodec.pyrimid_vertices;

      if (false) {
         for (unsigned int i_face_num=0; i_face_num<12; i_face_num++) {
            std::vector<unsigned int> face = p_dodec.d.face(i_face_num);
            std::cout << "face: i " << i_face_num << " ";
            for (unsigned int j=0; j<face.size(); j++) {
               std::cout << " " << face[j];
            }
            std::cout << std::endl;
            if (i_face_num != 0) continue;
         }
      }

      // kludge some vertices - the vertices are not shared between faces/triangles
      for (unsigned int i_face_num=0; i_face_num<12; i_face_num++) {
         std::vector<unsigned int> face = p_dodec.d.face(i_face_num);
         glm::vec3 sum(0,0,0);
         for (unsigned int j=0; j<5; j++)
            sum += clipper_to_glm(dodec_vertices_c[face[j]]);
         glm::vec3 n = glm::normalize(sum);
         for (unsigned int j=0; j<5; j++) {
            coot::api::vn_vertex v(clipper_to_glm(dodec_vertices_c[face[j]]), n);
            vertices.push_back(v);
         }
      }

      std::cout << "################# Here with vertices.size() " << vertices.size() << std::endl;
      for (unsigned int i=0; i<vertices.size(); i++)
         std::cout << i << " " << glm::to_string(vertices[i].pos) << std::endl;

      
      for (unsigned int i_face_num=0; i_face_num<12; i_face_num++) {
         unsigned int idx_base = i_face_num * 5;
         for (unsigned int j=1; j<=3; j++) {
            unsigned int index_this = j;
            unsigned int index_next = j + 1;
            g_triangle t(idx_base, idx_base + index_this, idx_base + index_next);
            triangles.push_back(t);
         }
      }
      
      std::cout << "################# Here with triangles.size() " << triangles.size() << std::endl;
      std::cout << "triangles:" << std::endl;
      for (unsigned int i=0; i<triangles.size(); i++) {
         std::cout << "triangle " << i << " :   "
                   << triangles[i].point_id[0] << " "
                   << triangles[i].point_id[1] << " "
                   << triangles[i].point_id[2] << " "
                   << std::endl;
      }
      std::cout << "face 0 vertices: " << std::endl;
      std::cout << " "
                << glm::to_string(vertices[triangles[0].point_id[0]].pos) << " "
                << glm::to_string(vertices[triangles[0].point_id[1]].pos) << " "
                << glm::to_string(vertices[triangles[0].point_id[2]].pos) << " "
                << std::endl;
      std::cout << " "
                << glm::to_string(vertices[triangles[1].point_id[0]].pos) << " "
                << glm::to_string(vertices[triangles[1].point_id[1]].pos) << " "
                << glm::to_string(vertices[triangles[1].point_id[2]].pos) << " "
                << std::endl;
      std::cout << " "
                << glm::to_string(vertices[triangles[2].point_id[0]].pos) << " "
                << glm::to_string(vertices[triangles[2].point_id[1]].pos) << " "
                << glm::to_string(vertices[triangles[2].point_id[2]].pos) << " "
                << std::endl;
   }
   return p;
}

std::pair<std::vector<coot::api::vn_vertex>, std::vector<g_triangle> >
make_pentakis_dodec(int spikey_mode) {

   std::pair<std::vector<coot::api::vn_vertex>, std::vector<g_triangle> > p;
   std::vector<coot::api::vn_vertex> &vertices = p.first;
   std::vector<g_triangle> &triangles = p.second;

   auto normal_from_vertices = [] (const glm::vec3 &pt_0,
                                   const glm::vec3 &pt_1,
                                   const glm::vec3 &pt_2) {
                                  glm::vec3 normal_for_flat_shading = glm::normalize(glm::cross((pt_1-pt_0), (pt_0-pt_2)));
                                  return normal_for_flat_shading;
                               };

   if (spikey_mode == DODEC_SPIKEY_MODE) {
      pentakis_dodec p_dodec(1.5);
      std::vector<clipper::Coord_orth>   dodec_vertices_c = p_dodec.d.coords();
      std::vector<clipper::Coord_orth> pyrimid_vertices_c = p_dodec.pyrimid_vertices;

      if (false) { // debugging
         for (unsigned int i_face_num=0; i_face_num<12; i_face_num++) {
            std::vector<unsigned int> face = p_dodec.d.face(i_face_num);
            std::cout << "face: i " << i_face_num << " ";
            for (unsigned int j=0; j<face.size(); j++) {
               std::cout << " " << face[j];
            }
            std::cout << std::endl;
            if (i_face_num != 0) continue;
         }
      }

      for (unsigned int i_face_num=0; i_face_num<12; i_face_num++) {
         std::vector<unsigned int> face = p_dodec.d.face(i_face_num);
         glm::vec3 point_point = clipper_to_glm(pyrimid_vertices_c[i_face_num]);
         for (unsigned int j=0; j<5; j++) {
            unsigned int index_this = j;
            unsigned int index_next = j + 1;
            if (index_next == 5) index_next = 0;
            glm::vec3 pt_1 = clipper_to_glm(dodec_vertices_c[face[index_this]]);
            glm::vec3 pt_2 = clipper_to_glm(dodec_vertices_c[face[index_next]]);
            glm::vec3 n = normal_from_vertices(point_point, pt_1, pt_2);
            vertices.push_back(coot::api::vn_vertex(point_point, n));
            vertices.push_back(coot::api::vn_vertex(pt_1, n));
            vertices.push_back(coot::api::vn_vertex(pt_2, n));
         }
      }

      for (unsigned int i_face_num=0; i_face_num<12; i_face_num++) {
         unsigned int idx_base = i_face_num * 15;
         for (unsigned int j=0; j<5; j++) {
            unsigned int idx_0 = 3 * j;
            unsigned int idx_1 = 3 * j + 1;
            unsigned int idx_2 = 3 * j + 2;
            if (idx_2 == 6) idx_2 = 1;
            g_triangle t(idx_base + idx_0, idx_base + idx_1, idx_base + idx_2);
            triangles.push_back(t);
         }
      }

   } else {

      // average the normals

      // go away squiggles.
      if (vertices.empty()) std::cout << "" << std::endl;
      if (triangles.empty()) std::cout << "" << std::endl;
   }

   return p;
}