ES-DE/external/CImg/examples/image_surface3d.cpp

141 lines
5.9 KiB
C++
Raw Normal View History

/*
#
# File : image_surface3d.cpp
# ( C++ source file )
#
# Description : This tool allows to show an image as a 3D surface.
# This file is a part of the CImg Library project.
# ( http://cimg.eu )
#
# Copyright : David Tschumperlé
# ( http://tschumperle.users.greyc.fr/ )
#
# License : CeCILL v2.0
# ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
#
# This software is governed by the CeCILL license under French law and
# abiding by the rules of distribution of free software. You can use,
# modify and/ or redistribute the software under the terms of the CeCILL
# license as circulated by CEA, CNRS and INRIA at the following URL
# "http://www.cecill.info".
#
# As a counterpart to the access to the source code and rights to copy,
# modify and redistribute granted by the license, users are provided only
# with a limited warranty and the software's author, the holder of the
# economic rights, and the successive licensors have only limited
# liability.
#
# In this respect, the user's attention is drawn to the risks associated
# with loading, using, modifying and/or developing or reproducing the
# software by the user in light of its specific status of free software,
# that may mean that it is complicated to manipulate, and that also
# therefore means that it is reserved for developers and experienced
# professionals having in-depth computer knowledge. Users are therefore
# encouraged to load and test the software's suitability as regards their
# requirements in conditions enabling the security of their systems and/or
# data to be ensured and, more generally, to use and operate it in the
# same conditions as regards security.
#
# The fact that you are presently reading this means that you have had
# knowledge of the CeCILL license and that you accept its terms.
#
*/
#include "CImg.h"
using namespace cimg_library;
#ifndef cimg_imagepath
#define cimg_imagepath "img/"
#endif
// Main procedure
//----------------
int main(int argc,char **argv) {
// Read command line arguments.
cimg_usage("Render an image as a surface");
const char *file_i = cimg_option("-i",cimg_imagepath "logo.bmp","Input image");
const char *file_o = cimg_option("-o",(char*)0,"Output 3D object");
const float sigma = cimg_option("-smooth",1.0f,"Amount of image smoothing");
const float ratioz = cimg_option("-z",0.25f,"Aspect ratio along z-axis");
const unsigned int di = cimg_option("-di",10,"Step for isophote skipping");
// Load 2D image file.
std::fprintf(stderr,"\n- Load file '%s'",cimg::basename(file_i)); std::fflush(stderr);
const CImg<unsigned char>
img = CImg<>(file_i).blur(sigma).resize(-100,-100,1,3),
norm = img.get_norm().normalize(0,255);
// Compute surface with triangles.
std::fprintf(stderr,"\n- Create image surface"); std::fflush(stderr);
CImgList<unsigned int> primitives;
CImgList<unsigned char> colors;
const CImg<> points = img.get_elevation3d(primitives,colors,norm*-ratioz);
// Compute image isophotes.
std::fprintf(stderr,"\n- Compute image isophotes"); std::fflush(stderr);
CImgList<unsigned int> isoprimitives;
CImgList<unsigned char> isocolors;
CImg<> isopoints;
for (unsigned int i = 0; i<255; i+=di) {
CImgList<> prims;
const CImg<> pts = norm.get_isoline3d(prims,(float)i);
isopoints.append_object3d(isoprimitives,pts,prims);
}
cimglist_for(isoprimitives,l) {
const unsigned int i0 = isoprimitives(l,0);
const float x0 = isopoints(i0,0), y0 = isopoints(i0,1);
const unsigned char
r = (unsigned char)img.linear_atXY(x0,y0,0),
g = (unsigned char)img.linear_atXY(x0,y0,1),
b = (unsigned char)img.linear_atXY(x0,y0,2);
isocolors.insert(CImg<unsigned char>::vector(r,g,b));
}
cimg_forX(isopoints,ll) isopoints(ll,2) = -ratioz*norm.linear_atXY(isopoints(ll,0),isopoints(ll,1));
// Save object if necessary
if (file_o) {
std::fprintf(stderr,"\n- Save 3d object as '%s'",cimg::basename(file_o)); std::fflush(stderr);
points.save_off(primitives,colors,file_o);
}
// Enter event loop
std::fprintf(stderr,
"\n- Enter interactive loop.\n\n"
"Reminder : \n"
" + Use mouse to rotate and zoom object\n"
" + key 'F' : Toggle fullscreen\n"
" + key 'Q' or 'ESC' : Quit\n"
" + Any other key : Change rendering type\n\n"); std::fflush(stderr);
const char *const title = "Image viewed as a surface";
CImgDisplay disp(800,600,title,0);
unsigned int rtype = 2;
CImg<float> pose = CImg<float>::identity_matrix(4);
while (!disp.is_closed()) {
const unsigned char white[3]={ 255, 255, 255 };
CImg<unsigned char> visu(disp.width(),disp.height(),1,3,0);
visu.draw_text(10,10,"%s",white,0,1,24,
rtype==0?"Points":(rtype==1?"Lines":(rtype==2?"Faces":(rtype==3?"Flat-shaded faces":
(rtype==4?"Gouraud-shaded faces":(rtype==5?"Phong-shaded faces":"Isophotes"))))));
static bool first_time = true;
if (rtype==6) visu.display_object3d(disp,isopoints,isoprimitives,isocolors,first_time,1,-1,true,
Squashed 'external/CImg/' changes from 4d061dcd6..049267da1 049267da1 . d974f83e1 . 143c6f781 . 8b641fa30 . 79ab52e6f . 593667dcf Auto-commit for release 3.2.6_pre a5305a976 . 6fa06681b . cdc9ee87a . 5f40398d1 Start work on next version 3.2.6. 78d1f0d7d Final release 3.2.5 620e4155c . be214a006 . 1f9896c3a Add support for 'id#ind' in math parser. 44800102a Add support for 'id#ind' in math parser. a6657d3d1 Auto-commit for release 3.2.5_pre 70e9a2cc3 . a77287fe0 . 05fd7a869 . 81c22bb46 . 1f14f5eff . bd1228f0f . 19e44e564 . 3f19980fc Start work on v.3.2.5. 504f5c653 . 26862961d Fix line width. 860b3747d Optimize cimg_math_parser::mp_vector_norm() by avoiding copies. 44eb192e9 . c8d8710ee . 1cf8f5c27 . 7b6ec0809 . 99871e252 Simplify CImg<T>::draw_polygon() and CImg<T>::draw_line(). 30ec22fde . a2c50471e . d2f63a9f8 Fix possible issue in 'CImg::draw_polygon()' (better fix). e0675260c Fix possible issue in 'CImg::draw_polygon()'. bb9980723 . c431612a1 . 3606c5f5b Auto-commit for release 3.2.4_pre d5d63e21f . acea91b06 . 9a3c772fa . ccb519bc0 . 9bf62d16b . 0936bc4b7 math evaluator: Add function 'unitnorm(V,p). 5236bd690 . 95ff538e1 . 3285740e9 . 7d01d9b70 Start work on 3.2.4 c7868e298 . 903cc805d . f03139316 math_parser(): Add function 'c2o()' (coordinates to offset). ff49ec20f math_parser(): Add function 'o2c()' (offset to coordinates). 6cbef2c1d Start working on version 3.2.3 ba9bb02a8 Final release 3.2.2 998d0157c . 1bb2d4f00 . 722646c42 Attempt to fix compilation on Windows. 2f3c64511 . 113a16334 . e67bdf5d9 . 31fe9269c . 64839222d Better support of 'CImg<unsigned long long>' and 'CImg<long long>'. 32b3bb6ff Add specific cimg::type() traits for 'unsigned long long' and 'long long'. 806ef2861 Start work on 3.2.2 b558be730 Final release 3.2.1 85aa2ac2e . bcafdc4d9 . a6961a5bd . 33b78048e . 3c063517e . 9c76c1842 . 87863667a . d60290f8d . 3b75fa436 . f1815c88a . 6f575e662 . 5d6be92b4 Allow dynamic arrays to have larger size (32bits integers, i.e 2G elements). c7d1772b8 . 977a1bb81 . 2cf046326 . 54d2b753c . f4a0f772d . d11a0380e . 4828190aa math_parser: Check that specified '#index' of functions are not NaN. 3b71fa0ce Function 'v2s()': Add option to 0-pad integer values. 652e6604b . ea3197ce4 Fix #374 c88e03819 math_parser: Add function that computes the square root of complex numbers. b58775209 Add detection of the not operator in fast pre-evaluation of math expressions. 5e3585553 Add detection of the not operator in fast pre-evaluation of math expressions. 209c38da9 . 6ba3d0a11 Fix #319. 4e9412e8d Start work on next version 3.2.1 57028920e . 9772e0204 . 446c39411 Math evaluator: Add new function 'abort()'. 3c2ecee1f Make fast string comparisons more robust when expressions contain blank characters. af9ecf8d0 Optimize evaluation of string comparisons. edfcda038 . 00cab3cea . a0af5312c . 6a92e46f2 . 7ae52c28d cimg_math_parser(): Add 'gamma()' function. ad516d976 . b71e2db76 . a9633d4ed . 9d8406e57 . 103d23625 . 6349c771f . bef50f9d8 . 391a24013 . d4669fbbc . 5e77a9c7e . 86440098e . 6b9eee37c . 616484469 . e79edfdb9 . e616c2443 . 772fa4dca . git-subtree-dir: external/CImg git-subtree-split: 049267da171c50ab48f93c7285f096d7d686e8f3
2023-08-05 08:49:38 +00:00
500.0f,0.0f,0.0f,-5000.0f,0.0f,0.0f,true,pose.data(),true);
else visu.display_object3d(disp,points,primitives,colors,first_time,rtype,-1,true,
Squashed 'external/CImg/' changes from 4d061dcd6..049267da1 049267da1 . d974f83e1 . 143c6f781 . 8b641fa30 . 79ab52e6f . 593667dcf Auto-commit for release 3.2.6_pre a5305a976 . 6fa06681b . cdc9ee87a . 5f40398d1 Start work on next version 3.2.6. 78d1f0d7d Final release 3.2.5 620e4155c . be214a006 . 1f9896c3a Add support for 'id#ind' in math parser. 44800102a Add support for 'id#ind' in math parser. a6657d3d1 Auto-commit for release 3.2.5_pre 70e9a2cc3 . a77287fe0 . 05fd7a869 . 81c22bb46 . 1f14f5eff . bd1228f0f . 19e44e564 . 3f19980fc Start work on v.3.2.5. 504f5c653 . 26862961d Fix line width. 860b3747d Optimize cimg_math_parser::mp_vector_norm() by avoiding copies. 44eb192e9 . c8d8710ee . 1cf8f5c27 . 7b6ec0809 . 99871e252 Simplify CImg<T>::draw_polygon() and CImg<T>::draw_line(). 30ec22fde . a2c50471e . d2f63a9f8 Fix possible issue in 'CImg::draw_polygon()' (better fix). e0675260c Fix possible issue in 'CImg::draw_polygon()'. bb9980723 . c431612a1 . 3606c5f5b Auto-commit for release 3.2.4_pre d5d63e21f . acea91b06 . 9a3c772fa . ccb519bc0 . 9bf62d16b . 0936bc4b7 math evaluator: Add function 'unitnorm(V,p). 5236bd690 . 95ff538e1 . 3285740e9 . 7d01d9b70 Start work on 3.2.4 c7868e298 . 903cc805d . f03139316 math_parser(): Add function 'c2o()' (coordinates to offset). ff49ec20f math_parser(): Add function 'o2c()' (offset to coordinates). 6cbef2c1d Start working on version 3.2.3 ba9bb02a8 Final release 3.2.2 998d0157c . 1bb2d4f00 . 722646c42 Attempt to fix compilation on Windows. 2f3c64511 . 113a16334 . e67bdf5d9 . 31fe9269c . 64839222d Better support of 'CImg<unsigned long long>' and 'CImg<long long>'. 32b3bb6ff Add specific cimg::type() traits for 'unsigned long long' and 'long long'. 806ef2861 Start work on 3.2.2 b558be730 Final release 3.2.1 85aa2ac2e . bcafdc4d9 . a6961a5bd . 33b78048e . 3c063517e . 9c76c1842 . 87863667a . d60290f8d . 3b75fa436 . f1815c88a . 6f575e662 . 5d6be92b4 Allow dynamic arrays to have larger size (32bits integers, i.e 2G elements). c7d1772b8 . 977a1bb81 . 2cf046326 . 54d2b753c . f4a0f772d . d11a0380e . 4828190aa math_parser: Check that specified '#index' of functions are not NaN. 3b71fa0ce Function 'v2s()': Add option to 0-pad integer values. 652e6604b . ea3197ce4 Fix #374 c88e03819 math_parser: Add function that computes the square root of complex numbers. b58775209 Add detection of the not operator in fast pre-evaluation of math expressions. 5e3585553 Add detection of the not operator in fast pre-evaluation of math expressions. 209c38da9 . 6ba3d0a11 Fix #319. 4e9412e8d Start work on next version 3.2.1 57028920e . 9772e0204 . 446c39411 Math evaluator: Add new function 'abort()'. 3c2ecee1f Make fast string comparisons more robust when expressions contain blank characters. af9ecf8d0 Optimize evaluation of string comparisons. edfcda038 . 00cab3cea . a0af5312c . 6a92e46f2 . 7ae52c28d cimg_math_parser(): Add 'gamma()' function. ad516d976 . b71e2db76 . a9633d4ed . 9d8406e57 . 103d23625 . 6349c771f . bef50f9d8 . 391a24013 . d4669fbbc . 5e77a9c7e . 86440098e . 6b9eee37c . 616484469 . e79edfdb9 . e616c2443 . 772fa4dca . git-subtree-dir: external/CImg git-subtree-split: 049267da171c50ab48f93c7285f096d7d686e8f3
2023-08-05 08:49:38 +00:00
500.0f,0.0f,0.0f,-5000.0f,0.0f,0.0f,true,pose.data(),true);
first_time = false;
switch (disp.key()) {
case 0: break;
case cimg::keyBACKSPACE: rtype = (7 + rtype - 1)%7; break;
case cimg::keyQ:
case cimg::keyESC: disp.close(); break;
case cimg::keyF:
if (disp.is_fullscreen()) disp.resize(800,600); else disp.resize(disp.screen_width(),disp.screen_height());
disp.toggle_fullscreen();
break;
default: rtype = (rtype + 1)%7; break;
}
}
return 0;
}