Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-30 07:55:41

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include <boost/test/tools/output_test_stream.hpp>
0010 #include <boost/test/unit_test.hpp>
0011 
0012 #include "Acts/Definitions/Algebra.hpp"
0013 #include "Acts/Visualization/IVisualization3D.hpp"
0014 #include "Acts/Visualization/ObjVisualization3D.hpp"
0015 #include "Acts/Visualization/PlyVisualization3D.hpp"
0016 
0017 #include <iostream>
0018 #include <string>
0019 #include <vector>
0020 
0021 #include "Visualization3DTester.hpp"
0022 
0023 using namespace Acts;
0024 
0025 namespace ActsTests {
0026 
0027 BOOST_AUTO_TEST_SUITE(VisualizationSuite)
0028 
0029 BOOST_AUTO_TEST_CASE(Visualization3DTesterObj) {
0030   // Test the tester
0031   std::string validObj = R"(# obj test file
0032 mtllib material.mtl
0033 usemtl material_a
0034 g rectangle
0035 vn 0 0 1
0036 vt 0 0 1
0037 v -15 -15 0
0038 v 15 -15 0
0039 v 15 15 0
0040 v -15 15 0
0041 f 1 2 3 4
0042 l 1 2
0043 l 2 3
0044 l 3 4
0045 l 4 1
0046 )";
0047 
0048   // Valid obj
0049   auto objErrors = testObjString(validObj);
0050   BOOST_CHECK(objErrors.empty());
0051 
0052   // Valid obj, but triangular mesh is requested
0053   objErrors = testObjString(validObj, true);
0054   BOOST_CHECK_EQUAL(objErrors.size(), 1);
0055   for (const auto& objerr : objErrors) {
0056     std::cout << objerr << std::endl;
0057   }
0058 
0059   // Non-valid obj - it has 4 errors
0060   std::string invalidObj = R"(# obj test file
0061 mtllib material.mtl
0062 usemtl material_a
0063 x whatever
0064 g rectangle
0065 vn 0 0 1
0066 vt 0 0 1
0067 v -15 -15 0 23
0068 v 15 -15 0
0069 v 15. 15 0
0070 v -15 15. 0
0071 f 1 2 3. 4
0072 l 0 2
0073 l 2 3
0074 l 3 4
0075 l 4 1
0076 )";
0077 
0078   objErrors = testObjString(invalidObj);
0079   BOOST_CHECK_EQUAL(objErrors.size(), 4);
0080   for (const auto& objerr : objErrors) {
0081     std::cout << objerr << std::endl;
0082   }
0083 }
0084 
0085 BOOST_AUTO_TEST_CASE(Visualization3DTesterPly) {
0086   // Test the tester
0087   std::string validPly = R"(ply
0088 format ascii 1.0
0089 comment made by Greg Turk
0090 comment this file is a cube
0091 element vertex 8
0092 property float x
0093 property float y
0094 property float z
0095 element face 6
0096 property list uchar int vertex_indices
0097 end_header
0098 0 0 0
0099 0 0 1
0100 0 1 1
0101 0 1 0
0102 1 0 0
0103 1 0 1
0104 1 1 1
0105 1 1 0
0106 4 0 1 2 3
0107 4 7 6 5 4
0108 4 0 4 5 1
0109 4 1 5 6 2
0110 4 2 6 7 3
0111 4 3 7 4 0
0112 )";
0113 
0114   // Valid ply
0115   auto plyErrors = testPlyString(validPly);
0116   BOOST_CHECK(plyErrors.empty());
0117 
0118   // Valid ply, but triangular mesh is requested
0119   plyErrors = testPlyString(validPly, true);
0120   BOOST_CHECK(plyErrors.empty());
0121   for (const auto& plyerr : plyErrors) {
0122     std::cout << plyerr << std::endl;
0123   }
0124 
0125   // Test the tester - contains 3 errors
0126   std::string invalidPly = R"(ply
0127 format ascii 1.0
0128 comment made by Greg Turk
0129 comment this file is a cube
0130 element vertex 8
0131 property float x
0132 property float y
0133 property float z
0134 element face 6
0135 property list uchar int vertex_indices
0136 whatever i write here
0137 end_header
0138 0 0 0 0
0139 0 0 1
0140 0 1 1
0141 0 1 0
0142 1 0 0
0143 1 0 1
0144 1 1 1
0145 1 1 0
0146 4 0 1 2 3
0147 4 7 6 5 4
0148 4 0 4 5 1
0149 4 1 5 6
0150 4 2 6 7 3
0151 4 3 7 4 0
0152 )";
0153 
0154   // Valid ply, but triangular mesh is requested
0155   plyErrors = testPlyString(invalidPly);
0156   BOOST_CHECK_EQUAL(plyErrors.size(), 3);
0157   for (const auto& plyerr : plyErrors) {
0158     std::cout << plyerr << std::endl;
0159   }
0160 }
0161 
0162 BOOST_AUTO_TEST_CASE(Visualization3DConstruction) {
0163   // this doesn't really test anything, other than conformance to the
0164   // IVisualization3D interface
0165   PlyVisualization3D ply;
0166   ObjVisualization3D obj;
0167 
0168   IVisualization3D* vis = nullptr;
0169   vis = &ply;
0170   std::cout << *vis << std::endl;
0171   vis = &obj;
0172   std::cout << *vis << std::endl;
0173 }
0174 
0175 BOOST_AUTO_TEST_CASE(PlyOutputTest) {
0176   PlyVisualization3D ply;
0177   boost::test_tools::output_test_stream output;
0178 
0179   ply.vertex({0, 0, 0});
0180 
0181   std::string exp = R"(ply
0182 format ascii 1.0
0183 element vertex 1
0184 property float x
0185 property float y
0186 property float z
0187 property uchar red
0188 property uchar green
0189 property uchar blue
0190 element face 0
0191 property list uchar int vertex_index
0192 element edge 0
0193 property int vertex1
0194 property int vertex2
0195 property uchar red
0196 property uchar green
0197 property uchar blue
0198 end_header
0199 0 0 0 120 120 120
0200 )";
0201 
0202   output << ply;
0203   BOOST_CHECK(output.is_equal(exp));
0204 
0205   ply.clear();
0206   ply.vertex({0, 1, 0});
0207 
0208   exp = R"(ply
0209 format ascii 1.0
0210 element vertex 1
0211 property float x
0212 property float y
0213 property float z
0214 property uchar red
0215 property uchar green
0216 property uchar blue
0217 element face 0
0218 property list uchar int vertex_index
0219 element edge 0
0220 property int vertex1
0221 property int vertex2
0222 property uchar red
0223 property uchar green
0224 property uchar blue
0225 end_header
0226 0 1 0 120 120 120
0227 )";
0228 
0229   output << ply;
0230   BOOST_CHECK(output.is_equal(exp));
0231 
0232   ply.clear();
0233   ply.line({0, 0, 1}, {1, 0, 0});
0234 
0235   output << ply;
0236 
0237   exp = R"(ply
0238 format ascii 1.0
0239 element vertex 2
0240 property float x
0241 property float y
0242 property float z
0243 property uchar red
0244 property uchar green
0245 property uchar blue
0246 element face 0
0247 property list uchar int vertex_index
0248 element edge 1
0249 property int vertex1
0250 property int vertex2
0251 property uchar red
0252 property uchar green
0253 property uchar blue
0254 end_header
0255 0 0 1 120 120 120
0256 1 0 0 120 120 120
0257 0 1 120 120 120
0258 )";
0259 
0260   BOOST_CHECK(output.is_equal(exp));
0261 
0262   ply.clear();
0263   ply.face({{1, 0, 0}, {1, 1, 0}, {0, 1, 0}});
0264 
0265   output << ply;
0266 
0267   exp = R"(ply
0268 format ascii 1.0
0269 element vertex 3
0270 property float x
0271 property float y
0272 property float z
0273 property uchar red
0274 property uchar green
0275 property uchar blue
0276 element face 1
0277 property list uchar int vertex_index
0278 element edge 0
0279 property int vertex1
0280 property int vertex2
0281 property uchar red
0282 property uchar green
0283 property uchar blue
0284 end_header
0285 1 0 0 120 120 120
0286 1 1 0 120 120 120
0287 0 1 0 120 120 120
0288 3 0 1 2
0289 )";
0290 
0291   BOOST_CHECK(output.is_equal(exp));
0292 }
0293 
0294 BOOST_AUTO_TEST_CASE(ObjOutputTest) {
0295   ObjVisualization3D obj;
0296 
0297   boost::test_tools::output_test_stream output;
0298 
0299   obj.vertex({1, 0, 0});
0300 
0301   output << obj;
0302 
0303   std::string exp = R"(usemtl material_120_120_120
0304 v 1 0 0
0305 )";
0306 
0307   BOOST_CHECK(output.is_equal(exp));
0308 
0309   obj.clear();
0310   obj.face({{1, 0, 0}, {1, 1, 0}, {0, 1, 0}});
0311   output << obj;
0312 
0313   exp = R"(usemtl material_120_120_120
0314 v 1 0 0
0315 v 1 1 0
0316 v 0 1 0
0317 usemtl material_120_120_120
0318 f 1 2 3
0319 )";
0320 
0321   BOOST_CHECK(output.is_equal(exp));
0322 }
0323 
0324 BOOST_AUTO_TEST_CASE(ColorTests) {
0325   Color red{"#ff0000"};
0326   BOOST_CHECK_EQUAL(red, Color(255, 0, 0));
0327 
0328   Color green{"#00ff00"};
0329   BOOST_CHECK_EQUAL(green, Color(0, 255, 0));
0330 
0331   Color blue{"#0000ff"};
0332   BOOST_CHECK_EQUAL(blue, Color(0, 0, 255));
0333 
0334   Color grey{"#808080"};
0335   BOOST_CHECK_EQUAL(grey, Color(128, 128, 128));
0336   BOOST_CHECK_EQUAL(grey, Color(std::array{128, 128, 128}));
0337   BOOST_CHECK_EQUAL(grey,
0338                     Color(std::array{128 / 255.0, 128 / 255.0, 128 / 255.0}));
0339   BOOST_CHECK_EQUAL(grey, Color(128 / 255.0, 128 / 255.0, 128 / 255.0));
0340 
0341   static_assert(Color{"#0000ff"} == Color(0, 0, 255));
0342 }
0343 
0344 BOOST_AUTO_TEST_SUITE_END()
0345 
0346 }  // namespace ActsTests