Giter Site home page Giter Site logo

isosurfaces's Introduction

Isosurfaces

Construct isolines/isosurfaces of a 2D/3D scalar field defined by a function, i.e. curves over which f(x,y)=0 or surfaces over which f(x,y,z)=0. Most similar libraries use marching squares or similar over a uniform grid, but this uses a quadtree to avoid wasting time sampling many far from the implicit surface.

This library is based on the approach described in Manson, Josiah, and Scott Schaefer. "Isosurfaces over simplicial partitions of multiresolution grids." Computer Graphics Forum. Vol. 29. No. 2. Oxford, UK: Blackwell Publishing Ltd, 2010.

An example graph, including quad lines, of y(x-y)^2 = 4x+8 (Python expression: y*(x-y)**2 - 4*x - 8)

Demo with grid lines

Installation

pip3 install isosurfaces

Usage

from isosurfaces import plot_isoline
import numpy as np

def f(x, y):
    return y * (x - y) ** 2 - 4 * x - 8

curves = plot_isoline(
    lambda u: f(u[0], u[1]),
    np.array([-8, -6]),
    np.array([8, 6]),
    # Increasing min_depth can help if you have small features
    min_depth=3,
    # Ensure max_quads is more than 4**min_depth to capture details better
    # than a 2**min_depth by 2**min_depth uniform grid
    max_quads=1000,
)

for curve in curves:
    print(', '.join(f"({p[0]:.3f},{p[1]:.3f})" for p in curve))

Dev examples

python3 isoline_demo.py && xdg-open out/demo.svg
manim -pql isosurface_demo.py --renderer=opengl --enable_gui

Pyflakes, allowing manim star imports

python3 -m pyflakes . | grep -v "star imports: manim"

Build source archive and wheel:

rm -rf dist build isosurfaces.egg-info
python3 setup.py sdist bdist_wheel
twine check dist/*
# for test:
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
# for actual
twine upload dist/*

Code formatting

isosurfaces uses black and isort. A Github Action will run to make sure it was applied.

Related

Related projects:

Other terms for an isoline:

  • Contour
  • Level curve
  • Topographic map

isosurfaces's People

Contributors

aheze avatar jared-hughes avatar viicos 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

Watchers

 avatar  avatar  avatar  avatar

isosurfaces's Issues

`active_curve` index out of range when graphing complicated functions

Hi! I came across your library and it works really well. But when I tried graphing the expression $\tan(x^2+y^2) = 1$, represented using this function:

def f(x, y):
    return np.tan(x ** 2 + y ** 2) - 1

... I got this:

Traceback (most recent call last):
  File "/Users/aheze/Downloads/isosurfaces-main/examples/isoline_demo.py", line 23, in <module>
    curves = CurveTracer(triangles, fn, tol).trace()
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/isosurfaces/isoline.py", line 234, in trace
    self.march_triangle(triangle)
  File "/opt/homebrew/lib/python3.11/site-packages/isosurfaces/isoline.py", line 256, in march_triangle
    self.active_curve.append(self.active_curve[0])
                             ~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

So I added a check to see if self.active_curve is empty inside march_triangle:

if closed_loop:
    
    # check if it's not empty
    if len(self.active_curve) > 0:
    
        # close back the loop
        self.active_curve.append(self.active_curve[0])

This time, it ran without crashing. Here's the result:

Multiple circles nested in each other with line fragments in between

There's a bunch of weird line fragments, probably because active_curve isn't being closed off. For reference here is the expected result from Desmos:

Expected result with multiple circles nested in each other

Do you have any idea what's going on? Again, thanks for your work on this library, it's exactly what I've been looking for!

License file

Hi!

I'm adding your package to the AUR because it has recently became a dependency for manim. I see that you're not distributing the package under any license, so I'm writing this issue to let you know of that fact and asking that you please inform me if you ever add it so that I can update the package asap :)

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.