Giter Club home page Giter Club logo

euclid's Introduction

euclid

R-CMD-check Codecov test coverage

The purpose of euclid is to provide a new unified foundation for computational geometry in R. It provides new data types for common low level geometric concepts such as lines, planes, points, triangles, etc. as well as a core of functionality relates to these primitives. Specialised geometric algorithms are intended to extend this package so that e.g. Tessellation will live in it’s own package but use the foundation laid out in euclid.

At its core euclid is an interface to the CGAL library for computational geometry. More specifically, it provides access to the functionality in the 2D and 3D Linear Geometry Kernel. The CGAL foundation means that computations with euclid are exact and free of the imprecision attached to working with floating point numbers. This is very important for geometry since floating point errors can compound and lead to failures in geometric predicates at the core of many geometric algorithms. To achieve this, data in euclid is never converted to R data structures but remain as pointers to CGAL structures unless specifically converted to numerics (in which case the floating point world kicks in)

Installation

For now, euclid can be installed from github using remotes:

# install.packages("remotes")
remotes::install_github("thomasp85/euclid")

Data types

The core of euclid is a set of new primitive vector types that model 2 and 3 dimensional geometric objects. The following types are present:

2 and 3 dimensions

  • Circles

  • Directions

  • Lines

  • Points

  • Weighted Points

  • Rays

  • Segments

  • Triangles

  • Vectors

2 dimensions

  • Iso rectangles

3 dimensions

  • Iso cubes

  • Planes

  • Spheres

  • Tetrahedrons

Special vector types

In addition to the geometric data types, euclid also provides these primitive vector types:

  • Exact numerics

  • Bounding boxes

  • Affine transformation matrices

Example

The following example shows how to work with the different data types:

library(euclid)

# Construct some exact numbers
random_num <- exact_numeric(rnorm(20))

# Exact numbers behave much like R numerics (though not everything is possible)
random_num[1:5]
#> <exact numerics [5]>
#> [1] -0.89691455  0.18484918  1.58784533 -1.13037567 -0.08025176
max(random_num)
#> <exact numerics [1]>
#> [1] 1.984474
random_num[2] * 10
#> <exact numerics [1]>
#> [1] 1.848492
random_num[5] + random_num[16]
#> <exact numerics [1]>
#> [1] -2.391321
sum(random_num)
#> <exact numerics [1]>
#> [1] 3.909221

# With exact numbers we can construct our geometries
p <- point(random_num[1:10], random_num[11:20])
p
#> <2D points [10]>
#>  [1] <x:-0.897, y:0.418> <x:0.185, y:0.982>  <x:1.59, y:-0.393> 
#>  [4] <x:-1.13, y:-1.04>  <x:-0.0803, y:1.78> <x:0.132, y:-2.31> 
#>  [7] <x:0.708, y:0.879>  <x:-0.24, y:0.0358> <x:1.98, y:1.01>   
#> [10] <x:-0.139, y:0.432>

# Create a line based on a vector, going through the origin
l <- line(point(0, 0), vec(3, 7))

# Which points lies on the positive side of the line?
p %is_on_positive_side% l
#>  [1]  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE

# Project points to line
p1 <- project(p, l)

# Do the projected points lie on the line?
p1 %is_on% l
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

# Construct a triangle from a random point to the extremes of the projected points
t <- triangle(point(rnorm(1), rnorm(1)), min(p1), max(p1))
t
#> <2D triangles [1]>
#> [1] [<x:2.09, y:-1.2>, <x:-0.816, y:-1.9>, <x:0.675, y:1.57>]

# Which points lies inside the triangle?
p %is_inside% t
#>  [1] FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE

# Visualise result
plot(p, pch = 16)
euclid_plot(t)
euclid_plot(p[p %is_inside% t], cex = 3)

# Area of t (cannot be given exact for all geometries so is returned as numerics)
approx_area(t)
#> [1] -4.531123

Code of Conduct

Please note that the euclid project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

euclid's People

Contributors

eeethb avatar jimhester avatar romainfrancois avatar thomasp85 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

euclid's Issues

Is there a geometry type for a region defined by a linear inequality?

Hi Thomas,

Thanks for the effort put into {euclid}. Great logo by the way!

Now, to the issue: can we represent the region $-x_1 + x_2 \leq -5$? And can we plot it? What about intersections of regions like these? Such functionality would be great to accompany exercises on linear programming.

unable to install euclid in R4.1.1

I double checked that the most recent Rtools has been installed.

I am unable to install euclid. The install process seems to be running indefinitely, but the following is a snapshot
image

a small typo in the Get Started page

A typo in the here

A vector can, as with points, be created from a set of x and y coordinates, but you could also created it from a vector of points:

could have also created or could also create?

Eucild install fails on Mac

Running on Intel Mac OS Ventura 13.6.3 , RStudio 2023.12.1+402, R (2023-10-31, Eye Holes)

First hurdle: installation failed 'cause mpfr not installed . Problem solved.

Second hurdle: I am getting the error log copied below with one error and one warning both outside my competence level!

Please advise.

Regards
M.P.

================================ Log copied from RSTUDIO
remotes::install_github("thomasp85/euclid")
Downloading GitHub repo thomasp85/euclid@HEAD
Running R CMD build...

  • checking for file ‘/private/var/folders/d9/0_vkl4tn39dg7ly6qjxblxfr0000gr/T/Rtmph2NaaT/remotes140c958ec5091/thomasp85-euclid-e9b7c93/DESCRIPTION’ ... OK
  • preparing ‘euclid’:
  • checking DESCRIPTION meta-information ... OK
  • cleaning src
  • running ‘cleanup’
  • checking for LF line-endings in source and make files and shell scripts
  • checking for empty or unneeded directories
  • building ‘euclid_0.0.0.9000.tar.gz’
  • installing source package ‘euclid’ ...
    ** using staged installation
    Found pkg-config cflags and libs!
    Using PKG_CFLAGS=-I/opt/local/include
    Using PKG_LIBS=-L/opt/local/lib -lmpfr -lgmp
    ** libs
    using C++ compiler: ‘Apple clang version 15.0.0 (clang-1500.1.0.2.5)’
    using C++14
    using SDK: ‘’
    clang++ -arch x86_64 -std=gnu++14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -DCGAL_DO_NOT_USE_BOOST_MP -DCGAL_USE_GMPXX -DCGAL_NDEBUG -I/opt/local/include -I../inst/include/internal/ -I'/Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cpp11/include' -I'/Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include' -I'/Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/BH/include' -I/opt/R/x86_64/include -fPIC -falign-functions=64 -Wall -g -O2 -c api.cpp -o api.o
    In file included from api.cpp:7:
    In file included from ../inst/include/internal/cgal_types.h:5:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Exact_predicates_exact_constructions_kernel.h:20:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Simple_cartesian.h:20:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Cartesian/Cartesian_base.h:62:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Cartesian/function_objects.h:20:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Kernel/function_objects.h:29:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/intersection_3.h:59:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Intersections_3/Plane_3_Triangle_3.h:25:
    /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Intersections_3/internal/Plane_3_Triangle_3_intersection.h:144:65: error: no member named 'prior' in namespace 'boost'
    k.construct_segment_3_object()(*pts.begin(), *boost::prior(pts.end())));
    ~~~~~~~^
    In file included from api.cpp:7:
    In file included from ../inst/include/internal/cgal_types.h:5:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Exact_predicates_exact_constructions_kernel.h:20:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Simple_cartesian.h:20:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Cartesian/Cartesian_base.h:62:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Cartesian/function_objects.h:20:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Kernel/function_objects.h:29:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/intersection_3.h:86:
    In file included from /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Intersections_3/Triangle_3_Triangle_3.h:26:
    /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/cgalh/include/CGAL/Intersections_3/internal/Triangle_3_Triangle_3_intersection.h:54:7: warning: variable 'pt_added' set but not used [-Wunused-but-set-variable]
    int pt_added = 0;
    ^
    1 warning and 1 error generated.
    make: *** [api.o] Error 1
    ERROR: compilation failed for package ‘euclid’
  • removing ‘/Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/euclid’
    Warning message:
    In i.p(...) :
    installation of package ‘/var/folders/d9/0_vkl4tn39dg7ly6qjxblxfr0000gr/T//Rtmph2NaaT/file140c9818dff9/euclid_0.0.0.9000.tar.gz’ had non-zero exit status

Installing euclid on older R versions

So I'm trying to install euclid on my windows machine that has R.3.6.3 installed. devtools is installed and working fine. My suspicion is cgal incompatibility with mingw , but can this be sorted purely at the R level?

C:/Users/.../R/R-3.6.3/library/cgal4h/include/CGAL/internal/enable_third_party_libraries.h:39:6: warning: #warning "<gmp.h> cannot be found. Less efficient number types will be used instead. Define CGAL_NO_GMP=1 if that is on purpose." [-Wcpp]
# warning "<gmp.h> cannot be found. Less efficient number types will be used instead. Define CGAL_NO_GMP=1 if that is on purpose."
^
In file included from C:/Users/.../R/R-3.6.3/library/cgal4h/include/CGAL/Rational_traits.h:32:0,
from C:/Users/.../R/R-3.6.3/library/cgal4h/include/CGAL/number_type_basic.h:49,
from C:/Users/.../R/R-3.6.3/library/cgal4h/include/CGAL/basic.h:45,
from C:/Users/.../R/R-3.6.3/library/cgal4h/include/CGAL/Cartesian/Cartesian_base.h:29,
from C:/Users/.../R/R-3.6.3/library/cgal4h/include/CGAL/Simple_cartesian.h:29,
from C:/Users/.../R/R-3.6.3/library/cgal4h/include/CGAL/Exact_predicates_exact_constructions_kernel.h:29,
from ../inst/include/internal/cgal_types.h:5,
from api.cpp:7:
C:/Users/.../R/R-3.6.3/library/cgal4h/include/CGAL/is_convertible.h:29:19: fatal error: gmpxx.h: No such file or directory
#include <gmpxx.h>
^
compilation terminated.
make: *** [C:/Users/.../DOCUME1/R/R-361.3/etc/i386/Makeconf:215: api.o] Error 1
ERROR: compilation failed for package 'euclid'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.