### Full CGAL SWIG Bindings Installation Example Source: https://github.com/cgal/cgal-swig-bindings/wiki/Installation This example demonstrates the complete process of cloning the repository, configuring with CMake, compiling, and running a Java example. Ensure CGAL is installed and its directory is correctly specified. ```bash > git clone https://github.com/cgal/cgal-swig-bindings > cd cgal-swig-bindings > mkdir build/CGAL-5.0_release -p > cd build/CGAL-5.0_release > cmake -DCGAL_DIR=/usr/lib/CGAL -DBUILD_PYTHON=OFF -DJAVA_OUTDIR_PREFIX=../../examples/java ../.. > make -j 4 > cd ../../examples/java > javac test_kernel.java > export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:lib > java test_kernel ... output of the example test_kernel.java ... ``` -------------------------------- ### Compile and Test Java Examples Source: https://github.com/cgal/cgal-swig-bindings/blob/main/CMakeLists.txt This snippet compiles Java example files and sets up custom targets for testing. It ensures necessary libraries are in the runtime path. ```cmake file (GLOB java_files "${CMAKE_CURRENT_SOURCE_DIR}/examples/java/*.java") set (dependencies Mesh2Criteria Mesh3FacetCriteria Mesh3CellCriteria) set (dependencies_files) foreach (TESTNAME ${dependencies}) list (APPEND dependencies_files ${CMAKE_CURRENT_SOURCE_DIR}/examples/java/${TESTNAME}.java) endforeach () foreach (TESTNAME ${dependencies}) list (REMOVE_ITEM java_files ${CMAKE_CURRENT_SOURCE_DIR}/examples/java/${TESTNAME}.java) endforeach () foreach (TESTNAME_SRC ${java_files}) get_filename_component(TESTNAME ${TESTNAME_SRC} NAME_WE) if(CGAL_ImageIO_FOUND OR NOT "${TESTNAME}" STREQUAL "test_surface_mesher") add_custom_target (javatest_${TESTNAME} COMMAND ${CMAKE_COMMAND} -E env "LD_LIBRARY_PATH=${JAVALIBPATH}" "DYLD_LIBRARY_PATH=${JAVALIBPATH}" ${Java_JAVAC_EXECUTABLE} ${TESTNAME_SRC} ${dependencies_files} -d ${JAVA_OUTDIR_PREFIX} WORKING_DIRECTORY ${JAVA_OUTDIR_PREFIX}) add_dependencies (tests javatest_${TESTNAME}) add_test (NAME javatest_${TESTNAME} COMMAND ${Java_JAVA_EXECUTABLE} ${TESTNAME} WORKING_DIRECTORY ${JAVA_OUTDIR_PREFIX}) set_tests_properties (javatest_${TESTNAME} PROPERTIES ENVIRONMENT "LD_LIBRARY_PATH=${JAVALIBPATH};DYLD_LIBRARY_PATH=${JAVALIBPATH}") endif() endforeach () ``` -------------------------------- ### Install CGAL Python Bindings Source: https://github.com/cgal/cgal-swig-bindings/blob/main/README.md Use pip to install the CGAL Python package. This is the recommended method for Python users. ```shell pip install cgal ``` -------------------------------- ### Halfedge Iterator Example Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Demonstrates iterating through halfedges in a HalfedgeDS structure using a for-each loop. ```Python from CGAL.CGAL_HalfedgeDS import HalfedgeDS from CGAL.CGAL_HalfedgeDS import HalfedgeDS_decorator from CGAL.CGAL_HalfedgeDS import HDS_Halfedge_handle hds = HalfedgeDS() decorator = HalfedgeDS_decorator(hds) decorator.create_loop() decorator.create_segment() assert decorator.is_valid() n = 0 for h in hds.halfedges(): n+=1 assert n == 4 ``` -------------------------------- ### Configure Python Specific Output Directory and Examples Source: https://github.com/cgal/cgal-swig-bindings/blob/main/CMakeLists.txt Sets the output directory prefix for Python-specific files and libraries. It also configures Python example tests, excluding specific examples. ```cmake if (BUILD_PYTHON) set (PYTHON_OUTDIR_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/build-python" CACHE PATH "PYTHON specific files output directory prefix: the CGAL directory will contain Python generated files and libraries.") configure_file (SWIG_CGAL/files/__init__.py "${PYTHON_OUTDIR_PREFIX}/CGAL/__init__.py" @ONLY) if (Python_EXECUTABLE) file (GLOB python_files "${CMAKE_SOURCE_DIR}/examples/python/*.py") # exclude blocking matplotlib plotting example list (REMOVE_ITEM python_files ${CMAKE_SOURCE_DIR}/examples/python/polygonal_triangulation.py) # exclude Intel TBB dependent example list (REMOVE_ITEM python_files ${CMAKE_SOURCE_DIR}/examples/python/Classification_example.py) foreach (TESTNAME_SRC ${python_files}) get_filename_component (TESTNAME ${TESTNAME_SRC} NAME_WE) add_test (NAME pythontest_${TESTNAME} COMMAND ${Python_EXECUTABLE} ${TESTNAME_SRC}) set_tests_properties (pythontest_${TESTNAME} PROPERTIES ENVIRONMENT "PYTHONPATH=${PYTHON_OUTDIR_PREFIX};DATADIR=${CMAKE_CURRENT_SOURCE_DIR}/examples/data") endforeach () endif () if (NOT DEFINED PYTHON_MODULE_PATH) if(Python_VERSION VERSION_LESS 3.12) execute_process (COMMAND ${Python_EXECUTABLE} -c "from distutils import sysconfig; print(sysconfig.get_python_lib(plat_specific=True, prefix='${CMAKE_INSTALL_PREFIX}'))" OUTPUT_VARIABLE _ABS_PYTHON_MODULE_PATH RESULT_VARIABLE _exit_code OUTPUT_STRIP_TRAILING_WHITESPACE ) if (NOT ${_exit_code}) get_filename_component (_ABS_PYTHON_MODULE_PATH ${_ABS_PYTHON_MODULE_PATH} ABSOLUTE) file (RELATIVE_PATH _REL_PYTHON_MODULE_PATH ${CMAKE_INSTALL_PREFIX} ${_ABS_PYTHON_MODULE_PATH}) ``` -------------------------------- ### Halfedge Iterator Example Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Demonstrates iterating through halfedges in a HalfedgeDS structure using a for-each loop. ```Java import CGAL.HalfedgeDS.HalfedgeDS; import CGAL.HalfedgeDS.HalfedgeDS_decorator; import CGAL.HalfedgeDS.HDS_Halfedge_handle; public class hds_prog_halfedge_iterator { public static void main(String arg[]){ HalfedgeDS hds = new HalfedgeDS(); HalfedgeDS_decorator decorator = new HalfedgeDS_decorator(hds); decorator.create_loop(); decorator.create_segment(); assert decorator.is_valid(); int n = 0; for ( HDS_Halfedge_handle h : hds.halfedges() ){ ++n; } assert n == 4; } } ``` -------------------------------- ### Java Triangulation_3 Example with File I/O Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Illustrates creating a 3D Delaunay triangulation, inserting points, locating points, and saving/reading the triangulation to/from a file in Java. Requires CGAL Java bindings. ```java import CGAL.Kernel.Point_3; import CGAL.Triangulation_3.Delaunay_triangulation_3; import CGAL.Triangulation_3.Delaunay_triangulation_3_Cell_handle; import CGAL.Triangulation_3.Delaunay_triangulation_3_Vertex_handle; import CGAL.Triangulation_3.Locate_type; import CGAL.Triangulation_3.Ref_Locate_type_3; import CGAL.Kernel.Ref_int; import java.util.LinkedList; import java.util.Vector; public class simple_triangulation_3 { public static void main(String arg[]){ LinkedList L=new LinkedList(); L.add( new Point_3(0,0,0) ); L.add( new Point_3(1,0,0) ); L.add( new Point_3(0,1,0) ); Delaunay_triangulation_3 T= new Delaunay_triangulation_3(L.iterator()); int n=T.number_of_vertices(); Vector V=new Vector(3); V.add( new Point_3(0,0,1) ); V.add( new Point_3(1,1,1) ); V.add( new Point_3(2,2,2) ); n = n + T.insert(V.iterator()); assert n==6; assert T.is_valid(); Ref_Locate_type_3 lt=new Ref_Locate_type_3(); Ref_int li=new Ref_int(); Ref_int lj=new Ref_int(); Point_3 p=new Point_3(0,0,0); Delaunay_triangulation_3_Cell_handle c = T.locate(p, lt, li, lj); assert lt.object() == Locate_type.VERTEX; assert c.vertex(li.object()).point().equals(p); Delaunay_triangulation_3_Vertex_handle v = c.vertex( (li.object()+1)&3 ); Delaunay_triangulation_3_Cell_handle nc = c.neighbor(li.object()); Ref_int nli=new Ref_int(); assert nc.has_vertex( v, nli ); T.write_to_file("output"); Delaunay_triangulation_3 T1 = new Delaunay_triangulation_3(); T1.read_from_file("output"); assert T1.is_valid(); assert T1.number_of_vertices() == T.number_of_vertices(); assert T1.number_of_cells() == T.number_of_cells(); } } ``` -------------------------------- ### Java Triangulation_2 Example Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Demonstrates basic triangulation operations in Java using CGAL's Triangulation_2 module. Requires CGAL Java bindings. ```java import CGAL.Kernel.Point_2; import CGAL.Triangulation_2.Triangulation_2; import CGAL.Triangulation_2.Triangulation_2_Vertex_circulator; import CGAL.Triangulation_2.Triangulation_2_Vertex_handle; import java.util.Vector; public class triangulation_prog1 { public static void main(String arg[]){ Vector points=new Vector(8); points.add( new Point_2(1,0) ); points.add( new Point_2(3,2) ); points.add( new Point_2(4,5) ); points.add( new Point_2(9,8) ); points.add( new Point_2(7,4) ); points.add( new Point_2(5,2) ); points.add( new Point_2(6,3) ); points.add( new Point_2(10,1) ); Triangulation_2 t=new Triangulation_2(); t.insert(points.iterator()); Triangulation_2_Vertex_circulator vc = t.incident_vertices(t.infinite_vertex()); if (vc.hasNext()) { Triangulation_2_Vertex_handle done = vc.next().clone(); Triangulation_2_Vertex_handle iter=null; do { iter=vc.next(); System.out.println(iter.point()); }while(!iter.equals(done)); } } } ``` -------------------------------- ### Python Triangulation_2 Example Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Demonstrates basic triangulation operations in Python using CGAL's Triangulation_2 module. Requires CGAL Python bindings. ```python from CGAL.CGAL_Kernel import Point_2 from CGAL.CGAL_Triangulation_2 import Triangulation_2 from CGAL.CGAL_Triangulation_2 import Triangulation_2_Vertex_circulator from CGAL.CGAL_Triangulation_2 import Triangulation_2_Vertex_handle points=[] points.append( Point_2(1,0) ) points.append( Point_2(3,2) ) points.append( Point_2(4,5) ) points.append( Point_2(9,8) ) points.append( Point_2(7,4) ) points.append( Point_2(5,2) ) points.append( Point_2(6,3) ) points.append( Point_2(10,1) ) t=Triangulation_2() t.insert(points) vc = t.incident_vertices(t.infinite_vertex()) if vc.hasNext(): done = vc.next(); iter=Triangulation_2_Vertex_handle() while(1): iter=vc.next() print iter.point() if iter == done: break ``` -------------------------------- ### Conforming Delaunay and Gabriel Triangulation in Java Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples This Java example constructs a constrained triangulation, makes it conforming Delaunay, and then conforming Gabriel, printing the vertex count at each stage. ```java import CGAL.Kernel.Point_2; import CGAL.Triangulation_2.Constrained_Delaunay_triangulation_2; import CGAL.Triangulation_2.Constrained_Delaunay_triangulation_2_Vertex_handle; import CGAL.Mesh_2.CGAL_Mesh_2; public class conforming { public static void main(String arg[]){ Constrained_Delaunay_triangulation_2 cdt=new Constrained_Delaunay_triangulation_2(); // construct a constrained triangulation Constrained_Delaunay_triangulation_2_Vertex_handle va = cdt.insert(new Point_2( 5., 5.)); Constrained_Delaunay_triangulation_2_Vertex_handle vb = cdt.insert(new Point_2(-5., 5.)); Constrained_Delaunay_triangulation_2_Vertex_handle vc = cdt.insert(new Point_2( 4., 3.)); Constrained_Delaunay_triangulation_2_Vertex_handle vd = cdt.insert(new Point_2( 5.,-5.)); Constrained_Delaunay_triangulation_2_Vertex_handle ve = cdt.insert(new Point_2( 6., 6.)); Constrained_Delaunay_triangulation_2_Vertex_handle vf = cdt.insert(new Point_2(-6., 6.)); Constrained_Delaunay_triangulation_2_Vertex_handle vg = cdt.insert(new Point_2(-6.,-6.)); Constrained_Delaunay_triangulation_2_Vertex_handle vh = cdt.insert(new Point_2( 6.,-6.)); cdt.insert_constraint(va,vb); cdt.insert_constraint(vb,vc); cdt.insert_constraint(vc,vd); cdt.insert_constraint(vd,va); cdt.insert_constraint(ve,vf); cdt.insert_constraint(vf,vg); cdt.insert_constraint(vg,vh); cdt.insert_constraint(vh,ve); System.out.println("Number of vertices before: "+ cdt.number_of_vertices()); // make it conforming Delaunay CGAL_Mesh_2.make_conforming_Delaunay_2(cdt); System.out.println("Number of vertices after make_conforming_Delaunay_2: "+cdt.number_of_vertices()); // then make it conforming Gabriel CGAL_Mesh_2.make_conforming_Gabriel_2(cdt); System.out.println("Number of vertices after make_conforming_Gabriel_2: "+cdt.number_of_vertices() ); } } ``` -------------------------------- ### SWIG Interface File Example Source: https://github.com/cgal/cgal-swig-bindings/wiki/Package_organization This is the main SWIG interface file used to generate bindings for a package. It includes globally defined SWIG macros and C++ macros for wrapping CGAL classes. ```swig %module CGAL_FOO //name of the package %include "SWIG_CGAL/common.i" //include globally defined SWIG macros Decl_void_type() //define C++ macros for exporting symbols (mainly for building shared libraries on Windows) SWIG_CGAL_add_java_loadLibrary(CGAL_FOO) //SWIG macro to automatically load the library CGAL_FOO when doing an import in Java. %import "SWIG_CGAL/Common/Macros.h" //C++ macros that helps wrapping CGAL classes ``` -------------------------------- ### AABB Polyhedron Facet Intersection Example Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Demonstrates constructing an AABB tree for a polyhedron and performing intersection queries with segments and planes. ```Python from CGAL.CGAL_Kernel import Point_3 from CGAL.CGAL_Kernel import Vector_3 from CGAL.CGAL_Kernel import Plane_3 from CGAL.CGAL_Kernel import Segment_3 from CGAL.CGAL_Polyhedron_3 import Polyhedron_3 from CGAL.CGAL_AABB_tree import AABB_tree_Polyhedron_3_Facet_handle p=Point_3(1.0, 0.0, 0.0) q=Point_3(0.0, 1.0, 0.0) r=Point_3(0.0, 0.0, 1.0) s=Point_3(0.0, 0.0, 0.0) polyhedron = Polyhedron_3() polyhedron.make_tetrahedron(p, q, r, s) #constructs AABB tree tree = AABB_tree_Polyhedron_3_Facet_handle(polyhedron.facets()) #constructs segment query a = Point_3(-0.2, 0.2, -0.2) b = Point_3(1.3, 0.2, 1.3) segment_query=Segment_3(a,b) #tests intersections with segment query if tree.do_intersect(segment_query): print "intersection(s)" else: print "no intersection" #computes #intersections with segment query print tree.number_of_intersected_primitives(segment_query)," intersection(s)" #computes first encountered intersection with segment query # (generally a point) intersection = tree.any_intersection(segment_query) if not intersection.empty(): #gets intersection object op = intersection.value() object = op[0] if object.is_Point_3(): print "intersection object is a point" #computes all intersections with segment query (as pairs object - primitive_id) intersections=[] tree.all_intersections(segment_query,intersections) #computes all intersected primitives with segment query as primitive ids primitives = [] tree.all_intersected_primitives(segment_query,primitives) #constructs plane query vec = Vector_3(0.0,0.0,1.0) plane_query = Plane_3(a,vec) #computes first encountered intersection with plane query # (generally a segment) intersection = tree.any_intersection(plane_query) if not intersection.empty(): #gets intersection object op = intersection.value() object = op[0] if object.is_Segment_3(): print "intersection object is a segment" ``` -------------------------------- ### Remove Outliers Example Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Demonstrates removing outliers from a point set read from a .xyz file. ```Python from sys import exit from CGAL.CGAL_Kernel import Point_3 from CGAL.CGAL_Kernel import Vector_3 from CGAL.CGAL_Point_set_processing_3 import * #Reads a .xyz point set file in points[]. points=[]; if not read_xyz_points("../data/oni.xyz", points): print "Error: cannot read file data/oni.xyz" exit() #Removes outliers using erase-remove idiom. removed_percentage = 5.0; # percentage of points to remove nb_neighbors = 24; # considers 24 nearest neighbor points new_size=remove_outliers(points, nb_neighbors, removed_percentage) points=points[0:new_size] ``` -------------------------------- ### Configure Installation RPATH for Libraries Source: https://github.com/cgal/cgal-swig-bindings/blob/main/CMakeLists.txt Sets the RPATH for installed libraries, which helps in locating shared libraries at runtime. The path differs for Apple and other platforms. ```cmake if(APPLE) set(CMAKE_INSTALL_RPATH "@loader_path;@loader_path/../lib;@loader_path/../../..") else() set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") endif() ``` -------------------------------- ### Remove Outliers Example Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Demonstrates removing outliers from a point set read from a .xyz file using the erase-remove idiom. ```Java import CGAL.Kernel.Point_3; import CGAL.Kernel.Vector_3; import CGAL.Point_set_processing_3.CGAL_Point_set_processing_3; import java.util.Vector; public class remove_outliers_example{ public static void main(String arg[]){ // Reads a .xyz point set file in points[]. Vector points = new Vector(); if ( !CGAL_Point_set_processing_3.read_xyz_points("../data/oni.xyz",points) ) { System.out.println("Error: cannot read file ../data/oni.xyz"); return; } // Removes outliers using erase-remove idiom. double removed_percentage = 5.0; // percentage of points to remove int nb_neighbors = 24; // considers 24 nearest neighbor points int new_size=CGAL_Point_set_processing_3.remove_outliers(points.iterator(), nb_neighbors, removed_percentage); points.setSize(new_size); } } ``` -------------------------------- ### 3D Delaunay Triangulation Creation and Operations Source: https://context7.com/cgal/cgal-swig-bindings/llms.txt Shows how to create a 3D Delaunay triangulation from a set of points, perform point location, navigate the cell structure, and handle file I/O. Also includes an example of creating a regular triangulation with weighted points. ```python from CGAL.CGAL_Kernel import Point_3, Weighted_point_3, Ref_int from CGAL.CGAL_Triangulation_3 import Delaunay_triangulation_3, Regular_triangulation_3 from CGAL.CGAL_Triangulation_3 import Ref_Locate_type_3, VERTEX # Create 3D Delaunay triangulation points = [ Point_3(0, 0, 0), Point_3(1, 0, 0), Point_3(0, 1, 0), Point_3(0, 0, 1), Point_3(1, 1, 1), Point_3(2, 2, 2) ] T = Delaunay_triangulation_3(points) print(f"Number of vertices: {T.number_of_vertices()}") # Output: 6 print(f"Number of cells: {T.number_of_cells()}") assert T.is_valid() # Point location with type information lt = Ref_Locate_type_3() li = Ref_int() lj = Ref_int() p = Point_3(0, 0, 0) cell = T.locate(p, lt, li, lj) if lt.object() == VERTEX: print(f"Point {p} is a vertex of the triangulation") print(f"Located at vertex index: {li.object()}") # Navigate cell structure for i in range(4): vertex = cell.vertex(i) print(f"Cell vertex {i}: {vertex.point()}") # File I/O T.write_to_file("triangulation_output") T2 = Delaunay_triangulation_3() T2.read_from_file("triangulation_output") assert T2.number_of_vertices() == T.number_of_vertices() # Regular triangulation with weighted points weighted_points = [] for z in range(3): for y in range(3): for x in range(3): p = Point_3(x, y, z) w = (x + y - z * y * x) * 2.0 # weight weighted_points.append(Weighted_point_3(p, w)) RT = Regular_triangulation_3() RT.insert(weighted_points) print(f"Regular triangulation vertices: {RT.number_of_vertices()}") ``` -------------------------------- ### Python Usage Example Source: https://github.com/cgal/cgal-swig-bindings/wiki/User_package Demonstrates how to use the custom `modify_polyhedron` function from the `CGAL_Dummy` module in Python. It shows creating a `Polyhedron_3` object, checking its size, modifying it, and checking the size again. ```python from CGAL import CGAL_Dummy from CGAL.CGAL_Polyhedron_3 import Polyhedron_3 p=Polyhedron_3() p.size_of_vertices() CGAL_Dummy.modify_polyhedron(p) p.size_of_vertices() ``` -------------------------------- ### Build CGAL Python Bindings from Source Source: https://context7.com/cgal/cgal-swig-bindings/llms.txt Follow these steps to build the CGAL SWIG bindings from source. Ensure you have CGAL 5.0+, SWIG 2.0+, and CMake 2.8+ installed. This process involves cloning the repository, configuring with CMake, and compiling. ```bash git clone https://github.com/cgal/cgal-swig-bindings cd cgal-swig-bindings mkdir build && cd build cmake -DCGAL_DIR=/usr/lib/CGAL -DBUILD_PYTHON=ON .. make -j4 ``` -------------------------------- ### Python Mapping for Input Iterators Source: https://github.com/cgal/cgal-swig-bindings/wiki/Mapping Demonstrates how input iterators are mapped to Python iterators and iterable containers. This example inserts a range of points into a Delaunay triangulation. ```python lst=[] lst.append(CGAL_Kernel.Point_2(0,0)) lst.append(CGAL_Kernel.Point_2(1,0)) lst.append(CGAL_Kernel.Point_2(1,1)) dt2=CGAL_Triangulation_2.Delaunay_triangulation_2() //insert a range of points to take advantage of the spatial sorting dt2.insert(lst) //or dt2.insert(lst.__iter__()) ``` -------------------------------- ### Build a Cube using Euler Operators (Java) Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Constructs a cube within a Polyhedron_3 object using CGAL's Euler operators. This example demonstrates edge splitting and facet splitting to build complex geometry. ```java import CGAL.Polyhedron_3.Polyhedron_3; import CGAL.Polyhedron_3.Polyhedron_3_Halfedge_handle; import CGAL.Kernel.Point_3; public class polyhedron_prog_cube{ public static Polyhedron_3_Halfedge_handle make_cube_3(Polyhedron_3 P){ // appends a cube of size [0,1]^3 to the polyhedron P. assert P.is_valid(); Polyhedron_3_Halfedge_handle h = P.make_tetrahedron( new Point_3( 1, 0, 0), new Point_3( 0, 0, 1), new Point_3( 0, 0, 0), new Point_3( 0, 1, 0)); Polyhedron_3_Halfedge_handle g = h.next().opposite().next(); P.split_edge( h.next() ); P.split_edge( g.next() ); P.split_edge( g ); h.next().vertex().set_point( new Point_3( 1, 0, 1) ); g.next().vertex().set_point( new Point_3( 0, 1, 1) ); g.opposite().vertex().set_point( new Point_3( 1, 1, 0) ); Polyhedron_3_Halfedge_handle f = P.split_facet(g.next(),g.next().next().next()); Polyhedron_3_Halfedge_handle e = P.split_edge(f); e.vertex().set_point( new Point_3( 1, 1, 1) ); P.split_facet( e, f.next().next()); assert P.is_valid(); return h; } public static void main(String arg[]){ Polyhedron_3 P=new Polyhedron_3(); Polyhedron_3_Halfedge_handle h = make_cube_3(P); assert !P.is_tetrahedron(h); } } ``` -------------------------------- ### Build a Cube using Euler Operators (Python) Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Constructs a cube within a Polyhedron_3 object using CGAL's Euler operators. This example demonstrates edge splitting and facet splitting to build complex geometry. ```python from CGAL.CGAL_Polyhedron_3 import Polyhedron_3 from CGAL.CGAL_Polyhedron_3 import Polyhedron_3_Halfedge_handle from CGAL.CGAL_Kernel import Point_3 def make_cube_3(P): # appends a cube of size [0,1]^3 to the polyhedron P. assert P.is_valid() h = P.make_tetrahedron(Point_3( 1, 0, 0),Point_3( 0, 0, 1),Point_3( 0, 0, 0),Point_3( 0, 1, 0)) g = h.next().opposite().next() P.split_edge( h.next() ) P.split_edge( g.next() ) P.split_edge( g ) h.next().vertex().set_point( Point_3( 1, 0, 1) ) g.next().vertex().set_point( Point_3( 0, 1, 1) ) g.opposite().vertex().set_point( Point_3( 1, 1, 0) ) f = P.split_facet(g.next(),g.next().next().next()) e = P.split_edge(f) e.vertex().set_point( Point_3( 1, 1, 1) ) P.split_facet( e, f.next().next()) assert P.is_valid() return h P = Polyhedron_3() h = make_cube_3(P) assert not P.is_tetrahedron(h) ``` -------------------------------- ### Refine 2D Constrained Delaunay Triangulation in Java Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Shows how to create a 2D constrained Delaunay triangulation, insert constraints, and then refine the mesh using global functions. This example requires CGAL Java bindings. ```java import CGAL.Kernel.Point_2; import CGAL.Mesh_2.Mesh_2_Constrained_Delaunay_triangulation_2; import CGAL.Mesh_2.Mesh_2_Constrained_Delaunay_triangulation_2_Vertex_handle; import CGAL.Mesh_2.Delaunay_mesh_size_criteria_2; import CGAL.Mesh_2.CGAL_Mesh_2; public class mesh_global { public static void main(String arg[]){ Mesh_2_Constrained_Delaunay_triangulation_2 cdt=new Mesh_2_Constrained_Delaunay_triangulation_2(); Mesh_2_Constrained_Delaunay_triangulation_2_Vertex_handle va = cdt.insert(new Point_2(-4,0)); Mesh_2_Constrained_Delaunay_triangulation_2_Vertex_handle vb = cdt.insert(new Point_2(0,-1)); Mesh_2_Constrained_Delaunay_triangulation_2_Vertex_handle vc = cdt.insert(new Point_2(4,0)); Mesh_2_Constrained_Delaunay_triangulation_2_Vertex_handle vd = cdt.insert(new Point_2(0,1)); cdt.insert(new Point_2(2, 0.6)); cdt.insert_constraint(va, vb); cdt.insert_constraint(vb, vc); cdt.insert_constraint(vc, vd); cdt.insert_constraint(vd, va); System.out.println("Number of vertices: "+cdt.number_of_vertices()); System.out.println("Meshing the triangulation..."); CGAL_Mesh_2.refine_Delaunay_mesh_2(cdt, new Delaunay_mesh_size_criteria_2(0.125, 0.5)); System.out.println("Number of vertices: "+cdt.number_of_vertices()); } } ``` -------------------------------- ### Python Mapping for Output Iterators Source: https://github.com/cgal/cgal-swig-bindings/wiki/Mapping Illustrates how output iterators are mapped to Python lists. This example collects all constrained edges incident to a vertex in a Constrained Delaunay triangulation. ```python edges=[] cdt2=CGAL_Triangulation_2.Constrained_Delaunay_triangulation_2() v=CGAL_Triangulation_2.Constrained_Delaunay_triangulation_2_Vertex_handle() //collect all constrained edges incident to a vertex cdt2.incident_constraints(v,edges); ``` -------------------------------- ### Python Enum Mapping Source: https://github.com/cgal/cgal-swig-bindings/wiki/Mapping Shows how C++ enums are wrapped as constants in Python. This example asserts that a point is on the bounded side of a sphere. ```python from CGAL.CGAL_Kernel import Point_3 from CGAL.CGAL_Kernel import Sphere_3 from CGAL.CGAL_Kernel import ON_BOUNDED_SIDE p=Point_3(0,0,0) s=Sphere_3(p,1) assert s.bounded_side(p)==ON_BOUNDED_SIDE ``` -------------------------------- ### Java Intersection Example with CGAL::Object Source: https://github.com/cgal/cgal-swig-bindings/wiki/Mapping Demonstrates intersecting two segments in Java using CGAL bindings. It shows how to use the `Object` type to retrieve the intersection result, which can be a `Point_3` in this case. Ensure necessary CGAL kernel classes are imported. ```Java import CGAL.Kernel.Point_3; import CGAL.Kernel.Segment_3; import CGAL.Kernel.CGAL_Kernel; import CGAL.Kernel.Object; .... Segment_3 seg1=new Segment_3(new Point_3(-1,0,0),new Point_3(1,0,0)); Segment_3 seg2=new Segment_3(new Point_3(0,1,0),new Point_3(0,-1,0)); Object obj=CGAL_Kernel.intersection(seg1,seg2); assert obj.is_Point_3(); System.out.println(obj.get_Point_3()); ``` -------------------------------- ### Configure User Package CMakeLists.txt Source: https://github.com/cgal/cgal-swig-bindings/wiki/User_package Create a `CMakeLists.txt` file for your user package. Use `ADD_SWIG_CGAL_JAVA_MODULE` and `ADD_SWIG_CGAL_PYTHON_MODULE` macros to specify the languages for which bindings should be generated. ```cmake ADD_SWIG_CGAL_JAVA_MODULE (Dummy) ADD_SWIG_CGAL_PYTHON_MODULE (Dummy) ``` -------------------------------- ### Configure Build with TBB Support Source: https://github.com/cgal/cgal-swig-bindings/blob/main/SWIG_CGAL/Alpha_wrap_3/CMakeLists.txt Sets up the build configuration, including linking CGAL kernel and optionally TBB libraries if found. This is a prerequisite for generating SWIG modules. ```cmake SET (LIBSTOLINKWITH CGAL_Kernel_cpp) if (TBB_FOUND) set(LIBSTOLINKWITH ${LIBSTOLINKWITH} TBB::tbb TBB::tbbmalloc Threads::Threads) endif() ``` -------------------------------- ### Create User Package Directory Source: https://github.com/cgal/cgal-swig-bindings/wiki/User_package Create a new directory within `SWIG_CGAL/User_packages` to host your custom wrapper. A symbolic link can be used if the package resides in a separate SCM repository. ```bash cd cgal-bindings mkdir SWIG_CGAL/User_packages/Dummy ``` -------------------------------- ### Determine Python Module Path Source: https://github.com/cgal/cgal-swig-bindings/blob/main/CMakeLists.txt This snippet determines the Python module installation path using `sysconfig`. It handles cases where the path needs to be adjusted relative to the installation prefix. ```cmake set (PYTHON_MODULE_PATH ${_REL_PYTHON_MODULE_PATH}) endif() else() execute_process (COMMAND ${Python_EXECUTABLE} -c "import sysconfig; print(sysconfig.get_path('platlib').removeprefix('/usr/local').removeprefix(sysconfig.get_config_var('base')).removeprefix(sysconfig.get_config_var('platbase')).removeprefix(sysconfig.get_config_var('installed_base')).removeprefix('/'))" OUTPUT_VARIABLE PYTHON_MODULE_PATH RESULT_VARIABLE _exit_code OUTPUT_STRIP_TRAILING_WHITESPACE ) endif() message(NOTICE "Python module path is ${PYTHON_MODULE_PATH}") if(_exit_code) message (SEND_ERROR "Could not run ${Python_EXECUTable}") endif () endif() #if not, libs will be installed in python/lib64/python/CGAL instead of python/CGAL if(${INSTALL_FROM_SETUP}) set (PYTHON_MODULE_PATH ".") endif() #TEMP : this part is kept in case the windows installation is broken. # if(NOT CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) # set(PYTHON_MODULE_PATH "${CMAKE_INSTALL_PREFIX}/python_install" CACHE PATH "Path to modules after installation") # else() # set(PYTHON_MODULE_PATH "${Python_SITEARCH}" CACHE PATH "Path to modules after installation") #endif() install (FILES ${PYTHON_OUTDIR_PREFIX}/CGAL/__init__.py DESTINATION ${PYTHON_MODULE_PATH}/CGAL) ``` -------------------------------- ### Enable SWIG and Configure Flags Source: https://github.com/cgal/cgal-swig-bindings/blob/main/CMakeLists.txt Enables SWIG if found and sets the SWIG include path. It also includes the SWIG use file. ```cmake if (SWIG_FOUND) SET (UseSWIG ON) INCLUDE(${SWIG_USE_FILE}) SET(CMAKE_SWIG_FLAGS "-I${CMAKE_CURRENT_SOURCE_DIR}") include_directories(${CMAKE_CURRENT_SOURCE_DIR}) ``` -------------------------------- ### Load and Save Polyhedron from OFF file Source: https://context7.com/cgal/cgal-swig-bindings/llms.txt Demonstrates loading a polyhedron from an OFF file and saving it to a new file. Requires an existing OFF file. ```python from CGAL.CGAL_Polyhedron_3 import Polyhedron_3 # Load polyhedron from OFF file mesh = Polyhedron_3("../data/elephant.off") print(f"Loaded mesh with {mesh.size_of_vertices()} vertices") # Save polyhedron to file mesh.write_to_file("output.off") ``` -------------------------------- ### Get all intersections with AABB tree Source: https://context7.com/cgal/cgal-swig-bindings/llms.txt Finds all intersection points between a segment and an AABB tree, storing them in a list. Requires the tree to be initialized. ```python all_intersections = [] facet_tree.all_intersections(segment, all_intersections) print(f"Total intersections: {len(all_intersections)}") ``` -------------------------------- ### Get first intersection with AABB tree Source: https://context7.com/cgal/cgal-swig-bindings/llms.txt Retrieves the first intersection point between a segment and an AABB tree. It checks if the intersection is a Point_3. ```python intersection = facet_tree.any_intersection(segment) if not intersection.empty(): op = intersection.value() obj = op[0] if obj.is_Point_3(): print("First intersection is a point") ``` -------------------------------- ### Create and Use 2D/3D Kernel Primitives in Python Source: https://context7.com/cgal/cgal-swig-bindings/llms.txt Demonstrates creating and manipulating 2D and 3D points, vectors, segments, and spheres using CGAL's kernel module. Requires importing necessary classes from CGAL_Kernel. ```python from CGAL.CGAL_Kernel import Point_2, Point_3, Vector_3, Segment_3, Plane_3, Sphere_3 from CGAL.CGAL_Kernel import CGAL_Kernel, ON_BOUNDED_SIDE # Create 2D points p1 = Point_2(0, 0) p2 = Point_2(3, 4) # Compute squared distance between points dist = CGAL_Kernel.squared_distance(p1, p2) print(f"Squared distance: {dist}") # Output: 25.0 # Create 3D points and vectors origin = Point_3(0, 0, 0) target = Point_3(1, 2, 3) vec = Vector_3(1, 0, 0) # Create a 3D segment segment = Segment_3(origin, target) print(f"Segment source: {segment.source()}") # Output: 0 0 0 # Create a sphere and test point inclusion center = Point_3(0, 0, 0) sphere = Sphere_3(center, 1) # radius squared = 1 test_point = Point_3(0.5, 0, 0) assert sphere.bounded_side(test_point) == ON_BOUNDED_SIDE # Create a plane from point and normal vector plane = Plane_3(origin, vec) # Plane through origin with normal (1,0,0) ``` -------------------------------- ### Python Iterator Mapping for Nested Iterators Source: https://github.com/cgal/cgal-swig-bindings/wiki/Mapping Shows how pseudo-nested iterators are mapped to Python iterators. This example iterates over finite vertices of a Delaunay triangulation. ```python dt2=CGAL_Triangulation_2.Delaunay_triangulation_2() for v in dt2.finite_vertices(): print v.point() ``` -------------------------------- ### Configure C++ Library with TBB and Threads Source: https://github.com/cgal/cgal-swig-bindings/blob/main/SWIG_CGAL/AABB_tree/CMakeLists.txt Sets up the core C++ library for SWIG bindings, including TBB and PThreads if found. This is a prerequisite for generating language-specific modules. ```cmake SET (LIBSTOLINKWITH CGAL_Kernel_cpp) if (TBB_FOUND) set(LIBSTOLINKWITH ${LIBSTOLINKWITH} TBB::tbb TBB::tbbmalloc Threads::Threads) endif() include_directories(include) if (TBB_FOUND) set(LIBSTOLINKWITH ${LIBSTOLINKWITH} TBB::tbb TBB::tbbmalloc Threads::Threads) endif() # cpp common library ADD_SWIG_CGAL_LIBRARY ( CGAL_AABB_tree_cpp Object.cpp ${LIBSTOLINKWITH} ) ``` -------------------------------- ### Create SWIG Interface File Source: https://github.com/cgal/cgal-swig-bindings/wiki/User_package Define the SWIG interface file (e.g., `CGAL_My_wrapper.i`) for your wrapper. This file includes common SWIG directives, imports necessary CGAL modules, and defines inline C++ functions to be exposed. ```swig %module CGAL_Dummy %include "SWIG_CGAL/common.i" Decl_void_type() SWIG_CGAL_add_java_loadLibrary(CGAL_Dummy) %import "SWIG_CGAL/Polyhedron_3/CGAL_Polyhedron_3.i" %{ #include #include %} SWIG_CGAL_import_Polyhedron_3_SWIG_wrapper %pragma(java) jniclassimports=%{import CGAL.Polyhedron_3.Polyhedron_3;%} %pragma(java) moduleimports =%{import CGAL.Polyhedron_3.Polyhedron_3;%} %inline %{ void modify_polyhedron(Polyhedron_3_SWIG_wrapper& polyhedron){ polyhedron.make_tetrahedron(Point_3::cpp_base(0,0,0), Point_3::cpp_base(0,0,1), Point_3::cpp_base(0,1,0), Point_3::cpp_base(1,1,1)); } %} ``` -------------------------------- ### Create and Manipulate 3D Delaunay Triangulation in Python Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Demonstrates creating a 3D Delaunay triangulation from a list of points, inserting additional points, locating points within the triangulation, and writing/reading the triangulation to/from a file. Requires CGAL Python bindings. ```python from CGAL.CGAL_Kernel import Point_3 from CGAL.CGAL_Triangulation_3 import Delaunay_triangulation_3 from CGAL.CGAL_Triangulation_3 import Delaunay_triangulation_3_Cell_handle from CGAL.CGAL_Triangulation_3 import Delaunay_triangulation_3_Vertex_handle from CGAL.CGAL_Triangulation_3 import VERTEX from CGAL.CGAL_Triangulation_3 import Ref_Locate_type_3 from CGAL.CGAL_Kernel import Ref_int L=[] L.append( Point_3(0,0,0) ) L.append( Point_3(1,0,0) ) L.append( Point_3(0,1,0) ) T=Delaunay_triangulation_3(L) n=T.number_of_vertices() V=[] V.append( Point_3(0,0,1) ) V.append( Point_3(1,1,1) ) V.append( Point_3(2,2,2) ) n = n + T.insert(V) assert n==6 assert T.is_valid() lt=Ref_Locate_type_3() li=Ref_int() lj=Ref_int() p=Point_3(0,0,0) c = T.locate(p, lt, li, lj) assert lt.object() == VERTEX assert c.vertex(li.object()).point() == p v = c.vertex( (li.object()+1)&3 ) nc = c.neighbor(li.object()) nli=Ref_int() assert nc.has_vertex( v, nli ) T.write_to_file("output") T1 = Delaunay_triangulation_3() T1.read_from_file("output") assert T1.is_valid() assert T1.number_of_vertices() == T.number_of_vertices() assert T1.number_of_cells() == T.number_of_cells() ``` -------------------------------- ### Create 3D Regular Triangulation in Java Source: https://github.com/cgal/cgal-swig-bindings/wiki/BindingsExamples Illustrates the creation of a 3D regular triangulation using weighted points. It inserts points, checks validity and dimension, and then removes all vertices. Requires CGAL Java bindings. ```java import CGAL.Kernel.Point_3; import CGAL.Kernel.Weighted_point_3; import CGAL.Triangulation_3.Regular_triangulation_3; import java.util.Vector; public class regular_3 { public static void main(String arg[]){ // generate points on a 3D grid Vector P=new Vector(); int number_of_points = 0; for (int z=0 ; z<5 ; z++) for (int y=0 ; y<5 ; y++) for (int x=0 ; x<5 ; x++) { Point_3 p = new Point_3(x, y, z); double w = (x+y-z*y*x)*2.0; // let's say this is the weight. P.add(new Weighted_point_3(p, w)); ++number_of_points; } Regular_triangulation_3 T = new Regular_triangulation_3(); // insert all points in a row (this is faster than one insert() at a time). T.insert (P.iterator()); assert T.is_valid(); assert T.dimension() == 3; System.out.println("Number of vertices : " + T.number_of_vertices()); // removal of all vertices int count = 0; while (T.number_of_vertices() > 0) { T.remove (T.finite_vertices().next()); ++count; } assert count == number_of_points; } } ```