Austin Schuh | 8c794d5 | 2019-03-03 21:17:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | # |
| 3 | # File : spherical_function3d.cpp |
| 4 | # ( C++ source file ) |
| 5 | # |
| 6 | # Description : An example that shows how to build custom 3D objects in CImg. |
| 7 | # This file is a part of the CImg Library project. |
| 8 | # ( http://cimg.eu ) |
| 9 | # |
| 10 | # Copyright : David Tschumperle |
| 11 | # ( http://tschumperle.users.greyc.fr/ ) |
| 12 | # |
| 13 | # License : CeCILL v2.0 |
| 14 | # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html ) |
| 15 | # |
| 16 | # This software is governed by the CeCILL license under French law and |
| 17 | # abiding by the rules of distribution of free software. You can use, |
| 18 | # modify and/ or redistribute the software under the terms of the CeCILL |
| 19 | # license as circulated by CEA, CNRS and INRIA at the following URL |
| 20 | # "http://www.cecill.info". |
| 21 | # |
| 22 | # As a counterpart to the access to the source code and rights to copy, |
| 23 | # modify and redistribute granted by the license, users are provided only |
| 24 | # with a limited warranty and the software's author, the holder of the |
| 25 | # economic rights, and the successive licensors have only limited |
| 26 | # liability. |
| 27 | # |
| 28 | # In this respect, the user's attention is drawn to the risks associated |
| 29 | # with loading, using, modifying and/or developing or reproducing the |
| 30 | # software by the user in light of its specific status of free software, |
| 31 | # that may mean that it is complicated to manipulate, and that also |
| 32 | # therefore means that it is reserved for developers and experienced |
| 33 | # professionals having in-depth computer knowledge. Users are therefore |
| 34 | # encouraged to load and test the software's suitability as regards their |
| 35 | # requirements in conditions enabling the security of their systems and/or |
| 36 | # data to be ensured and, more generally, to use and operate it in the |
| 37 | # same conditions as regards security. |
| 38 | # |
| 39 | # The fact that you are presently reading this means that you have had |
| 40 | # knowledge of the CeCILL license and that you accept its terms. |
| 41 | # |
| 42 | */ |
| 43 | |
| 44 | #include "CImg.h" |
| 45 | using namespace cimg_library; |
| 46 | |
| 47 | /*--------------------------- |
| 48 | |
| 49 | Main procedure |
| 50 | |
| 51 | --------------------------*/ |
| 52 | int 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 | CImgList<unsigned int> object_primitives(12,1,2,1,1, // Define the 12 segments of the cube |
| 68 | 0,1, 1,2, 2,3, 3,0, |
| 69 | 4,5, 5,6, 6,7, 7,4, |
| 70 | 0,4, 1,5, 2,6, 3,7); |
| 71 | object_colors.insert(object_primitives.size(),CImg<unsigned char>::vector(32,64,255)); |
| 72 | object_opacities.insert(object_primitives.size(),CImg<float>::vector(0.3f)); |
| 73 | |
| 74 | // Define the spherical function's vertices. |
| 75 | CImgList<float> spherical_vertices; |
| 76 | const float a = 1; |
| 77 | const unsigned int na = 132, nb = 132; |
| 78 | for (unsigned int v = 0; v<na; ++v) |
| 79 | for (unsigned int u = 0; u<nb; ++u) { |
| 80 | const float |
| 81 | alpha = (float)(u*2*cimg::PI/nb), |
| 82 | beta = (float)(-cimg::PI/2 + v*cimg::PI/na), |
| 83 | x = (float)((a*std::cos(beta))*std::cos(alpha)), |
| 84 | y = (float)((a*std::cos(beta))*std::sin(alpha)), |
| 85 | z = (float)(a*std::sin(beta)), |
| 86 | altitude = cimg::abs(1 + 0.8f*std::cos(3*alpha)*std::sin(4*beta)); |
| 87 | spherical_vertices.insert(CImg<float>::vector(altitude*x,altitude*y,altitude*z)); |
| 88 | } |
| 89 | |
| 90 | // Define the spherical function's mesh. |
| 91 | CImgList<unsigned int> spherical_primitives; |
| 92 | for (unsigned int vv = 0; vv<na - 1; ++vv) |
| 93 | for (unsigned int uu = 0; uu<nb; ++uu) { |
| 94 | const int nv = (vv + 1)%na, nu = (uu + 1)%nb; |
| 95 | spherical_primitives.insert(CImg<unsigned int>::vector(nb*vv + nu,nb*nv + uu,nb*vv + uu)); |
| 96 | spherical_primitives.insert(CImg<unsigned int>::vector(nb*vv + nu,nb*nv + nu,nb*nv + uu)); |
| 97 | object_colors.insert(CImg<>::vector(0,255,255)); |
| 98 | object_colors.insert(CImg<>::vector(100,200,255)); |
| 99 | object_opacities.insert(2,CImg<>::vector(1)); |
| 100 | } |
| 101 | |
| 102 | // Merge 3D objects together. |
| 103 | object_vertices.append_object3d(object_primitives,spherical_vertices>'x',spherical_primitives); |
| 104 | char title[4096] = { 0 }; |
| 105 | std::sprintf(title,"3D Spherical Function (%u vertices, %u primitives)", |
| 106 | object_vertices.width(),object_primitives.size()); |
| 107 | CImgDisplay disp(640,480,title,0); |
| 108 | CImg<unsigned char>(disp.width(),disp.height(),1,3,220). |
| 109 | display_object3d(disp,object_vertices,object_primitives,object_colors,object_opacities,true,4,3,false, |
| 110 | 500,0,0,-5000,0.1f,1.5f); |
| 111 | |
| 112 | return 0; |
| 113 | } |