From 2dc4e9adff18934fab86c9272e253d3254259d39 Mon Sep 17 00:00:00 2001 From: Cary Phillips Date: Tue, 11 Oct 2022 15:02:07 -0700 Subject: [PATCH] Fix example code so it compiles as is (#260) * Fix example code so it compiles as is * Add missing includes * Make intro example actually do something sensible, and move to intro.cpp * Add building the example code to the CI Signed-off-by: Cary Phillips * Add missing add_test Signed-off-by: Cary Phillips Signed-off-by: Cary Phillips --- .github/workflows/ci_workflow.yml | 18 ++++++++++++++---- docs/examples/CMakeLists.txt | 17 +++++++++++++++++ docs/examples/Color3.cpp | 1 + docs/examples/Color4.cpp | 1 + docs/examples/Euler.cpp | 3 ++- docs/examples/Frustum.cpp | 1 + docs/examples/Interval.cpp | 1 + docs/examples/Line3.cpp | 1 + docs/examples/Matrix22.cpp | 1 + docs/examples/Matrix33.cpp | 1 + docs/examples/Matrix44.cpp | 1 + docs/examples/Plane3.cpp | 1 + docs/examples/Quat.cpp | 1 + docs/examples/Sphere3.cpp | 1 + docs/examples/Vec2.cpp | 1 + docs/examples/Vec3.cpp | 1 + docs/examples/Vec4.cpp | 1 + docs/examples/intro.cpp | 24 ++++++++++++++++++++++++ docs/intro.rst | 21 +-------------------- 19 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 docs/examples/intro.cpp diff --git a/.github/workflows/ci_workflow.yml b/.github/workflows/ci_workflow.yml index 876b7cb0..662b05ac 100644 --- a/.github/workflows/ci_workflow.yml +++ b/.github/workflows/ci_workflow.yml @@ -284,6 +284,9 @@ jobs: working-directory: _build - name: Examples run: | + # Confirm the python module loads. Query the site-packages directory and substitute ../_install + export PYTHONPATH=`python -c "import site; print('../_install%s' % site.USER_SITE[len(site.USER_BASE):])"` + python -c "import imath;print(imath.__version__)" # Make sure we can build the tests when configured as a # standalone application linking against the just-installed # Imath library. @@ -295,10 +298,17 @@ jobs: cmake --build . \ --config ${{ matrix.build-type }} # Confirm the tests run - #./bin/ImathTest - # Confirm the python module loads. Query the site-packages directory and substitute ../_install - export PYTHONPATH=`python -c "import site; print('../_install%s' % site.USER_SITE[len(site.USER_BASE):])"` - python -c "import imath;print(imath.__version__)" + ./bin/ImathTest + # Confirm the examples compile and execute + rm -rf bin CMakeCache.txt CMakeFiles cmake_install.cmake Makefile + cmake ../docs/examples \ + -DCMAKE_PREFIX_PATH=../../_install \ + -DCMAKE_BUILD_TYPE=${{ matrix.build-type }} \ + -DCMAKE_CXX_STANDARD=${{ matrix.cxx-standard }} \ + -DCMAKE_CXX_FLAGS=${{ matrix.cxx-flags }} + cmake --build . \ + --config ${{ matrix.build-type }} + ctest working-directory: _examples - name: Test run: | diff --git a/docs/examples/CMakeLists.txt b/docs/examples/CMakeLists.txt index 94a8a723..4a6a8175 100644 --- a/docs/examples/CMakeLists.txt +++ b/docs/examples/CMakeLists.txt @@ -7,6 +7,10 @@ project(imath-examples) find_package(Imath) +# The introductory example on the main docs page +add_executable(imath-intro intro.cpp) + +# A main() that executes all the example code snippets add_executable(imath-examples main.cpp Color3.cpp @@ -28,4 +32,17 @@ add_executable(imath-examples half.cpp ) +target_link_libraries(imath-intro Imath::Imath) target_link_libraries(imath-examples Imath::Imath) + +set_target_properties(imath-examples PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" +) +set_target_properties(imath-intro PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" +) + +add_test(NAME Imath.imath-intro COMMAND $) +add_test(NAME Imath.imath-examples COMMAND $) + +enable_testing() diff --git a/docs/examples/Color3.cpp b/docs/examples/Color3.cpp index 4eee2219..9d4e625c 100644 --- a/docs/examples/Color3.cpp +++ b/docs/examples/Color3.cpp @@ -1,4 +1,5 @@ #include +#include void color3_example () diff --git a/docs/examples/Color4.cpp b/docs/examples/Color4.cpp index 882cc73e..c40792e1 100644 --- a/docs/examples/Color4.cpp +++ b/docs/examples/Color4.cpp @@ -1,4 +1,5 @@ #include +#include void color4_example () diff --git a/docs/examples/Euler.cpp b/docs/examples/Euler.cpp index 80eca975..664a20d6 100644 --- a/docs/examples/Euler.cpp +++ b/docs/examples/Euler.cpp @@ -1,5 +1,6 @@ #include -#include +#include +#include void euler_example () diff --git a/docs/examples/Frustum.cpp b/docs/examples/Frustum.cpp index a8f0257e..5742dfd9 100644 --- a/docs/examples/Frustum.cpp +++ b/docs/examples/Frustum.cpp @@ -1,4 +1,5 @@ #include +#include void frustum_example () diff --git a/docs/examples/Interval.cpp b/docs/examples/Interval.cpp index 3e798972..00db073c 100644 --- a/docs/examples/Interval.cpp +++ b/docs/examples/Interval.cpp @@ -1,4 +1,5 @@ #include +#include void interval_example () diff --git a/docs/examples/Line3.cpp b/docs/examples/Line3.cpp index 15d9a9b8..5dfa1e19 100644 --- a/docs/examples/Line3.cpp +++ b/docs/examples/Line3.cpp @@ -1,4 +1,5 @@ #include +#include void line3_example () diff --git a/docs/examples/Matrix22.cpp b/docs/examples/Matrix22.cpp index 44fb5d85..e52ae5bf 100644 --- a/docs/examples/Matrix22.cpp +++ b/docs/examples/Matrix22.cpp @@ -1,5 +1,6 @@ #include #include +#include void matrix22_example () diff --git a/docs/examples/Matrix33.cpp b/docs/examples/Matrix33.cpp index 989af48f..3439b5db 100644 --- a/docs/examples/Matrix33.cpp +++ b/docs/examples/Matrix33.cpp @@ -1,5 +1,6 @@ #include #include +#include void matrix33_example () diff --git a/docs/examples/Matrix44.cpp b/docs/examples/Matrix44.cpp index b68f8a42..a582569d 100644 --- a/docs/examples/Matrix44.cpp +++ b/docs/examples/Matrix44.cpp @@ -1,5 +1,6 @@ #include #include +#include void matrix44_example () diff --git a/docs/examples/Plane3.cpp b/docs/examples/Plane3.cpp index b4771ea3..d75f732e 100644 --- a/docs/examples/Plane3.cpp +++ b/docs/examples/Plane3.cpp @@ -1,4 +1,5 @@ #include +#include void plane3_example () diff --git a/docs/examples/Quat.cpp b/docs/examples/Quat.cpp index 719936c0..66d1cd1e 100644 --- a/docs/examples/Quat.cpp +++ b/docs/examples/Quat.cpp @@ -1,4 +1,5 @@ #include +#include void quat_example () diff --git a/docs/examples/Sphere3.cpp b/docs/examples/Sphere3.cpp index 717adb91..244a6c21 100644 --- a/docs/examples/Sphere3.cpp +++ b/docs/examples/Sphere3.cpp @@ -1,4 +1,5 @@ #include +#include void sphere3_example () diff --git a/docs/examples/Vec2.cpp b/docs/examples/Vec2.cpp index 1b2e5ec7..f4bc0f26 100644 --- a/docs/examples/Vec2.cpp +++ b/docs/examples/Vec2.cpp @@ -1,4 +1,5 @@ #include +#include void vec2_example () diff --git a/docs/examples/Vec3.cpp b/docs/examples/Vec3.cpp index 9925bd36..c72ea8be 100644 --- a/docs/examples/Vec3.cpp +++ b/docs/examples/Vec3.cpp @@ -1,4 +1,5 @@ #include +#include void vec3_example () diff --git a/docs/examples/Vec4.cpp b/docs/examples/Vec4.cpp index f459ede4..5ee1ab0d 100644 --- a/docs/examples/Vec4.cpp +++ b/docs/examples/Vec4.cpp @@ -1,4 +1,5 @@ #include +#include void vec4_example () diff --git a/docs/examples/intro.cpp b/docs/examples/intro.cpp new file mode 100644 index 00000000..eaa24257 --- /dev/null +++ b/docs/examples/intro.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +int +main() +{ + const Imath::V3f v (3.0f, 4.0f, 5.0f); + + Imath::M44f M; + const Imath::V3f t(1.0f, 2.0f, 3.0f); + M.translate (t); + + Imath::V3f p; + M.multVecMatrix(v, p); + + std::cout << "What's your vector, Victor? " << p << std::endl; + + Imath::V3f vt = v + t; + assert (p.equalWithAbsError(vt, 1e-6f)); + + return 0; +} diff --git a/docs/intro.rst b/docs/intro.rst index 9e3d4b2d..3c2e71a6 100644 --- a/docs/intro.rst +++ b/docs/intro.rst @@ -27,26 +27,7 @@ Example A basic program: -.. code-block:: - - #include - #include - - int - main() - { - Imath::V3f v (3.0, 4.0, 5.0); - - v.normalize(); - - Imath::M33f M; - M.translate (1.0, 2.0, 3.0); - Imath::V3f p = v * M; - - std::cout << "What's your vector, Victor? " << p << std::endl; - - return 0; - } +.. literalinclude:: examples/intro.cpp Matrices Are Row-Major ----------------------