File indexing completed on 2026-04-09 07:48:58
0001
0002 #include <iostream>
0003 #include <sstream>
0004 #include <iomanip>
0005
0006 #include <glm/gtx/transform.hpp>
0007 #include "NP.hh"
0008 #include "CSGView.h"
0009
0010 void CSGView::update(const glm::vec4& em, const glm::vec4& ce, const unsigned width, const unsigned height)
0011 {
0012 eye_model = em ;
0013 center_extent = ce ;
0014
0015 glm::vec3 tr(ce.x, ce.y, ce.z);
0016 glm::vec3 sc(ce.w);
0017 glm::vec3 isc(1.f/ce.w);
0018
0019 glm::mat4 model2world = glm::scale(glm::translate(glm::mat4(1.0), tr), sc);
0020
0021
0022
0023 glm::vec4 eye_m( em.x,em.y,em.z,1.f);
0024 glm::vec4 look_m( 0.f, 0.f,0.f,1.f);
0025 glm::vec4 up_m( 0.f, 0.f,1.f,1.f);
0026 glm::vec4 gze_m( look_m - eye_m ) ;
0027
0028 const glm::mat4& m2w = model2world ;
0029
0030 eye = m2w * eye_m ;
0031 look = m2w * look_m ;
0032 up = m2w * up_m ;
0033 gaze = m2w * gze_m ;
0034
0035 glm::vec3 forward_ax = glm::normalize(glm::vec3(gaze));
0036 glm::vec3 right_ax = glm::normalize(glm::cross(forward_ax,glm::vec3(up)));
0037 glm::vec3 top_ax = glm::normalize(glm::cross(right_ax,forward_ax));
0038
0039 float aspect = float(width)/float(height) ;
0040 float tanYfov = 1.f ;
0041 float gazelength = glm::length( glm::vec3(gaze) ) ;
0042 float v_half_height = gazelength * tanYfov ;
0043 float u_half_width = v_half_height * aspect ;
0044
0045 U = glm::vec4( right_ax * u_half_width, 0.f) ;
0046 V = glm::vec4( top_ax * v_half_height, 0.f) ;
0047 W = glm::vec4( forward_ax * gazelength, 0.f) ;
0048 }
0049
0050 void CSGView::dump(const char* msg) const
0051 {
0052 std::cout << msg << std::endl ;
0053 std::cout << desc("eye_model", eye_model ) << std::endl;
0054 std::cout << desc("center_extent", center_extent ) << std::endl;
0055 std::cout << desc("eye", eye ) << std::endl;
0056 std::cout << desc("look", look ) << std::endl;
0057 std::cout << desc("up", up ) << std::endl;
0058 std::cout << desc("gaze", gaze ) << std::endl;
0059 std::cout << desc("U", U ) << std::endl;
0060 std::cout << desc("V", V ) << std::endl;
0061 std::cout << desc("W", W ) << std::endl;
0062 }
0063
0064 std::string CSGView::desc( const char* label, const glm::vec4& v )
0065 {
0066 std::stringstream ss ;
0067 ss
0068 << std::setw(20) << label
0069 << " ( "
0070 << std::setw(10) << std::fixed << std::setprecision(3) << v.x
0071 << std::setw(10) << std::fixed << std::setprecision(3) << v.y
0072 << std::setw(10) << std::fixed << std::setprecision(3) << v.z
0073 << std::setw(10) << std::fixed << std::setprecision(3) << v.w
0074 << " ) "
0075 ;
0076
0077 std::string s = ss.str();
0078 return s ;
0079 }
0080
0081 void CSGView::save(const char* dir) const
0082 {
0083 std::cout << "CSGView::save " << dir << std::endl ;
0084 NP view("<f4", 9, 4);
0085 float* f = view.values<float>() ;
0086
0087 std::vector<std::string> names ;
0088
0089 collect4(f, 0, eye_model); names.push_back("eye_model");
0090 collect4(f, 1, center_extent); names.push_back("center_extent");
0091 collect4(f, 2, eye); names.push_back("eye");
0092 collect4(f, 3, look); names.push_back("look");
0093 collect4(f, 4, up); names.push_back("up");
0094 collect4(f, 5, gaze); names.push_back("gaze");
0095 collect4(f, 6, U); names.push_back("U");
0096 collect4(f, 7, V); names.push_back("V");
0097 collect4(f, 8, W); names.push_back("W");
0098 view.save(dir, "view.npy");
0099
0100 NP::WriteNames(dir, "view.txt", names);
0101 }
0102
0103 void CSGView::collect4( float* f, unsigned i, const glm::vec4& v )
0104 {
0105 *(f+4*i+0) = v.x ;
0106 *(f+4*i+1) = v.y ;
0107 *(f+4*i+2) = v.z ;
0108 *(f+4*i+3) = v.w ;
0109 }
0110
0111