MCPcopy Create free account
hub / github.com/GreycLab/CImg / main

Function main

examples/spherical_function3d.cpp:52–114  ·  view source on GitHub ↗

--------------------------- Main procedure --------------------------*/

Source from the content-addressed store, hash-verified

50
51 --------------------------*/
52int main() {
53
54 CImgList<unsigned char> object_colors;
55 CImgList<float> object_opacities;
56
57 // Define a 3D centered box.
58 CImg<float> object_vertices = CImg<float>(3,8,1,1, // Define the 8 vertices of the cube
59 -1,-1,-1, // (x0,y0,z0)
60 1,-1,-1, // (x1,y1,z1)
61 1,1,-1, // ...
62 -1,1,-1,
63 -1,-1,1,
64 1,-1,1,
65 1,1,1, // (x6,y6,z6)
66 -1,1,1).transpose(); // (x7,y7,z7)
67
68 // Define the 12 segments of the cube.
69 CImgList<unsigned int> object_primitives =
70 CImg<unsigned int>(1,12*2,1,1, 0,1, 1,2, 2,3, 3,0, 4,5, 5,6, 6,7, 7,4, 0,4, 1,5, 2,6, 3,7).get_split('y',12);
71
72 object_colors.insert(object_primitives.size(),CImg<unsigned char>::vector(32,64,255));
73 object_opacities.insert(object_primitives.size(),CImg<float>::vector(0.3f));
74
75 // Define the spherical function's vertices.
76 CImgList<float> spherical_vertices;
77 const float a = 1;
78 const unsigned int na = 132, nb = 132;
79 for (unsigned int v = 0; v<na; ++v)
80 for (unsigned int u = 0; u<nb; ++u) {
81 const float
82 alpha = (float)(u*2*cimg::PI/nb),
83 beta = (float)(-cimg::PI/2 + v*cimg::PI/na),
84 x = (float)((a*std::cos(beta))*std::cos(alpha)),
85 y = (float)((a*std::cos(beta))*std::sin(alpha)),
86 z = (float)(a*std::sin(beta)),
87 altitude = cimg::abs(1 + 0.8f*std::cos(3*alpha)*std::sin(4*beta));
88 spherical_vertices.insert(CImg<float>::vector(altitude*x,altitude*y,altitude*z));
89 }
90
91 // Define the spherical function's mesh.
92 CImgList<unsigned int> spherical_primitives;
93 for (unsigned int vv = 0; vv<na - 1; ++vv)
94 for (unsigned int uu = 0; uu<nb; ++uu) {
95 const int nv = (vv + 1)%na, nu = (uu + 1)%nb;
96 spherical_primitives.insert(CImg<unsigned int>::vector(nb*vv + nu,nb*nv + uu,nb*vv + uu));
97 spherical_primitives.insert(CImg<unsigned int>::vector(nb*vv + nu,nb*nv + nu,nb*nv + uu));
98 object_colors.insert(CImg<>::vector(0,255,255));
99 object_colors.insert(CImg<>::vector(100,200,255));
100 object_opacities.insert(2,CImg<>::vector(1));
101 }
102
103 // Merge 3D objects together.
104 object_vertices.append_object3d(object_primitives,spherical_vertices>'x',spherical_primitives);
105 char title[4096] = { 0 };
106 std::sprintf(title,"3D Spherical Function (%u vertices, %u primitives)",
107 object_vertices.width(),object_primitives.size());
108 CImgDisplay disp(640,480,title,0);
109 CImg<unsigned char>(disp.width(),disp.height(),1,3,220).

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected