Skip to content

Commit

Permalink
Fix example code so it compiles as is (#260)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Add missing add_test

Signed-off-by: Cary Phillips <[email protected]>

Signed-off-by: Cary Phillips <[email protected]>
  • Loading branch information
cary-ilm authored Oct 11, 2022
1 parent 3ad5d4d commit 2dc4e9a
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 25 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/ci_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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: |
Expand Down
17 changes: 17 additions & 0 deletions docs/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 $<TARGET_FILE:imath-intro>)
add_test(NAME Imath.imath-examples COMMAND $<TARGET_FILE:imath-examples>)

enable_testing()
1 change: 1 addition & 0 deletions docs/examples/Color3.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Imath/ImathColor.h>
#include <cassert>

void
color3_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Color4.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Imath/ImathColor.h>
#include <cassert>

void
color4_example ()
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/Euler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Imath/ImathEuler.h>
#include <ImathMatrixAlgo.h>
#include <Imath/ImathMatrixAlgo.h>
#include <cassert>

void
euler_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Frustum.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Imath/ImathFrustum.h>
#include <cassert>

void
frustum_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Interval.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Imath/ImathInterval.h>
#include <cassert>

void
interval_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Line3.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Imath/ImathLine.h>
#include <cassert>

void
line3_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Matrix22.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Imath/ImathMatrix.h>
#include <Imath/ImathMatrixAlgo.h>
#include <cassert>

void
matrix22_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Matrix33.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Imath/ImathMatrix.h>
#include <Imath/ImathMatrixAlgo.h>
#include <cassert>

void
matrix33_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Matrix44.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <Imath/ImathMatrix.h>
#include <Imath/ImathMatrixAlgo.h>
#include <cassert>

void
matrix44_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Plane3.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Imath/ImathPlane.h>
#include <cassert>

void
plane3_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Quat.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Imath/ImathQuat.h>
#include <cassert>

void
quat_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Sphere3.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Imath/ImathSphere.h>
#include <cassert>

void
sphere3_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Vec2.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Imath/ImathVec.h>
#include <cassert>

void
vec2_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Vec3.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Imath/ImathVec.h>
#include <cassert>

void
vec3_example ()
Expand Down
1 change: 1 addition & 0 deletions docs/examples/Vec4.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Imath/ImathVec.h>
#include <cassert>

void
vec4_example ()
Expand Down
24 changes: 24 additions & 0 deletions docs/examples/intro.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <Imath/ImathMatrix.h>
#include <Imath/ImathVec.h>
#include <iostream>
#include <cassert>

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;
}
21 changes: 1 addition & 20 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,7 @@ Example

A basic program:

.. code-block::
#include <Imath/ImathVec.h>
#include <iostream>
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
----------------------
Expand Down

0 comments on commit 2dc4e9a

Please sign in to comment.