Skip to content

Commit

Permalink
Merge pull request #309 from hx2A/fix302
Browse files Browse the repository at this point in the history
Fix #302
  • Loading branch information
hx2A authored Jun 24, 2023
2 parents 6a7a3bf + c25733a commit 6d89f7e
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 34 deletions.
4 changes: 4 additions & 0 deletions generate_py5.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def run_code_builder(name, clsname, class_name=None):

logger.info(f'reading Py5Vector code')
py5vector_method_signatures = find_signatures('Py5Vector', Path('py5_resources/py5_module/py5/vector.py'))
py5graphics_method_signatures = find_signatures('Py5Graphics', Path('py5_resources/py5_module/py5/graphics.py'))
py5shape_method_signatures = find_signatures('Py5Shape', Path('py5_resources/py5_module/py5/shape.py'))

# this assembles the code fragments from the builders so it can be
# inserted into the code templates to complete the py5 module.
Expand Down Expand Up @@ -150,6 +152,8 @@ def run_code_builder(name, clsname, class_name=None):
**py5keyevent_builder.method_signatures,
**py5mouseevent_builder.method_signatures,
**py5vector_method_signatures,
**py5graphics_method_signatures,
**py5shape_method_signatures,
**ref.EXTRA_METHOD_SIGNATURES,
}

Expand Down
2 changes: 2 additions & 0 deletions generator/codebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ def find_signatures(class_name, filename):
if decorator == '@overload':
method_signatures[(class_name, fname)].append((COMMA_REGEX.split(args), rettypestr))
elif (class_name, fname) not in overloaded:
if args == '*args':
continue
split_args = COMMA_REGEX.split(args) if args else []
method_signatures[(class_name, fname)].append((split_args, rettypestr))

Expand Down
32 changes: 32 additions & 0 deletions py5_docs/Reference/api_en/Py5Shape_bezier_vertices.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@@ meta
name = bezier_vertices()
type = method

@@ signatures
bezier_vertices(coordinates: npt.NDArray[np.floating], /) -> None

@@ variables
coordinates: npt.NDArray[np.floating] - 2D array of bezier vertex coordinates with 6 or 9 columns for 2D or 3D points, respectively

@@ description
Create a collection of bezier vertices. The purpose of this method is to provide an alternative to repeatedly calling [](py5shape_bezier_vertex) in a loop. For a large number of bezier vertices, the performance of `bezier_vertices()` will be much faster.

The `coordinates` parameter should be a numpy array with one row for each bezier vertex. The first few columns are for the first control point, the next few columns are for the second control point, and the final few columns are for the anchor point. There should be six or nine columns for 2D or 3D points, respectively.

Drawing 2D bezier curves requires using the `P2D` renderer and drawing 3D bezier curves requires using the `P3D` renderer. When drawing directly with `Py5Shape` objects, bezier curves do not work at all using the default renderer.

This method can only be used within a [](py5shape_begin_shape) and [](py5shape_end_shape) pair.

@@ example
image = Py5Shape_bezier_vertices_0.png

import numpy as np

def setup():
py5.size(100, 100, py5.P2D)
random_bezier_vertices = 100 * np.random.rand(25, 6)
s = py5.create_shape()
with s.begin_shape():
s.vertex(py5.width / 2, py5.height / 2)
s.bezier_vertices(random_bezier_vertices)
py5.shape(s)
2 changes: 0 additions & 2 deletions py5_docs/Reference/api_en/Py5Shape_curve_vertex.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ Specifies a `Py5Shape` object's vertex coordinates for curves. This method may o

Drawing 2D curves requires using the `P2D` renderer and drawing 3D curves requires using the `P3D` renderer. When drawing directly with `Py5Shape` objects, curves do not work at all using the default renderer.

This method can only be used within a [](py5shape_begin_shape) and [](py5shape_end_shape) pair.

@@ example
image = Sketch_curve_vertex_0.png

Expand Down
32 changes: 32 additions & 0 deletions py5_docs/Reference/api_en/Py5Shape_curve_vertices.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@@ meta
name = curve_vertices()
type = method

@@ signatures
curve_vertices(coordinates: npt.NDArray[np.floating], /) -> None

@@ variables
coordinates: npt.NDArray[np.floating] - 2D array of curve vertex coordinates with 2 or 3 columns for 2D or 3D points, respectively

@@ description
Create a collection of curve vertices. The purpose of this method is to provide an alternative to repeatedly calling [](py5shape_curve_vertex) in a loop. For a large number of curve vertices, the performance of `curve_vertices()` will be much faster.

The `coordinates` parameter should be a numpy array with one row for each curve vertex. There should be two or three columns for 2D or 3D points, respectively.

Drawing 2D curves requires using the `P2D` renderer and drawing 3D curves requires using the `P3D` renderer. When drawing directly with `Py5Shape` objects, curves do not work at all using the default renderer.

This method can only be used within a [](py5shape_begin_shape) and [](py5shape_end_shape) pair.

@@ example
image = Py5Shape_curve_vertices_0.png

import numpy as np

def setup():
py5.size(100, 100, py5.P2D)
random_curve_vertices = 100 * np.random.rand(25, 2)
s = py5.create_shape()
with s.begin_shape():
s.vertex(py5.width / 2, py5.height / 2)
s.curve_vertices(random_curve_vertices)
py5.shape(s)
30 changes: 30 additions & 0 deletions py5_docs/Reference/api_en/Py5Shape_quadratic_vertices.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
@@ meta
name = quadratic_vertices()
type = method

@@ signatures
quadratic_vertices(coordinates: npt.NDArray[np.floating], /) -> None

@@ variables
coordinates: npt.NDArray[np.floating] - 2D array of quadratic vertex coordinates with 4 or 6 columns for 2D or 3D points, respectively

@@ description
Create a collection of quadratic vertices. The purpose of this method is to provide an alternative to repeatedly calling [](py5shape_quadratic_vertex) in a loop. For a large number of quadratic vertices, the performance of `quadratic_vertices()` will be much faster.

The `coordinates` parameter should be a numpy array with one row for each quadratic vertex. The first few columns are for the control point and the next few columns are for the anchor point. There should be four or six columns for 2D or 3D points, respectively.

Drawing 2D bezier curves requires using the `P2D` renderer and drawing 3D bezier curves requires using the `P3D` renderer. When drawing directly with `Py5Shape` objects, bezier curves do not work at all using the default renderer.

@@ example
image = Py5Shape_quadratic_vertices_0.png

import numpy as np

def setup():
py5.size(100, 100, py5.P2D)
random_quadratic_vertices = 100 * np.random.rand(25, 4)
s = py5.create_shape()
with s.begin_shape():
s.vertex(py5.width / 2, py5.height / 2)
s.quadratic_vertices(random_quadratic_vertices)
py5.shape(s)
26 changes: 26 additions & 0 deletions py5_docs/Reference/api_en/Py5Shape_vertices.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@@ meta
name = vertices()
type = method

@@ signatures
vertices(coordinates: npt.NDArray[np.floating], /) -> None

@@ variables
coordinates: npt.NDArray[np.floating] - 2D array of vertex coordinates with 2 or 3 columns for 2D or 3D points, respectively

@@ description
Create a collection of vertices. The purpose of this method is to provide an alternative to repeatedly calling [](py5shape_vertex) in a loop. For a large number of vertices, the performance of `vertices()` will be much faster.

The `coordinates` parameter should be a numpy array with one row for each vertex. There should be two or three columns for 2D or 3D points, respectively.

@@ example
image = Py5Shape_vertices_0.png

import numpy as np

def setup():
random_triangle_vertices = 100 * np.random.rand(25, 2)
s = py5.create_shape()
with s.begin_shape(s.TRIANGLES):
s.vertices(random_triangle_vertices)
py5.shape(s)
7 changes: 3 additions & 4 deletions py5_docs/Reference/api_en/Sketch_bezier_vertices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import numpy as np

def setup():
random_bezier_vertices = 100 * np.random.rand(25, 6)
py5.begin_shape()
py5.vertex(py5.width / 2, py5.height / 2)
py5.bezier_vertices(random_bezier_vertices)
py5.end_shape()
with py5.begin_shape():
py5.vertex(py5.width / 2, py5.height / 2)
py5.bezier_vertices(random_bezier_vertices)
7 changes: 3 additions & 4 deletions py5_docs/Reference/api_en/Sketch_curve_vertices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import numpy as np

def setup():
random_curve_vertices = 100 * np.random.rand(25, 2)
py5.begin_shape()
py5.vertex(py5.width / 2, py5.height / 2)
py5.curve_vertices(random_curve_vertices)
py5.end_shape()
with py5.begin_shape():
py5.vertex(py5.width / 2, py5.height / 2)
py5.curve_vertices(random_curve_vertices)
7 changes: 3 additions & 4 deletions py5_docs/Reference/api_en/Sketch_quadratic_vertices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import numpy as np

def setup():
random_quadratic_vertices = 100 * np.random.rand(25, 4)
py5.begin_shape()
py5.vertex(py5.width / 2, py5.height / 2)
py5.quadratic_vertices(random_quadratic_vertices)
py5.end_shape()
with py5.begin_shape():
py5.vertex(py5.width / 2, py5.height / 2)
py5.quadratic_vertices(random_quadratic_vertices)
5 changes: 2 additions & 3 deletions py5_docs/Reference/api_en/Sketch_vertices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ import numpy as np

def setup():
random_triangle_vertices = 100 * np.random.rand(25, 2)
py5.begin_shape(py5.TRIANGLES)
py5.vertices(random_triangle_vertices)
py5.end_shape()
with py5.begin_shape(py5.TRIANGLES):
py5.vertices(random_triangle_vertices)
4 changes: 2 additions & 2 deletions py5_jar/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="lib" path="/home/jim/INSTALL/processing-4/core/library/core.jar"/>
<classpathentry kind="lib" path="/home/jim/INSTALL/processing-4/core/library/jogl-all.jar"/>
<classpathentry kind="lib" path="/Users/jim/INSTALL/Processing.app/Contents/Java/core.jar"/>
<classpathentry kind="lib" path="/Users/jim/INSTALL/Processing.app/Contents/Java/core/library/jogl-all.jar"/>
<classpathentry kind="output" path="build"/>
</classpath>
113 changes: 113 additions & 0 deletions py5_jar/src/main/java/py5/core/Py5ShapeHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/******************************************************************************
Part of the py5 library
Copyright (C) 2020-2023 Jim Schmitz
This library is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2.1 of the License, or (at
your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library. If not, see <https://www.gnu.org/licenses/>.
******************************************************************************/
package py5.core;

import processing.core.PShape;

public class Py5ShapeHelper {

public static void vertices(PShape s, float[][] coordinates) {
if (coordinates.length == 0) {
// no vertices
return;
}

if (coordinates[0].length == 2) {
for (int i = 0; i < coordinates.length; ++i) {
s.vertex(coordinates[i][0], coordinates[i][1]);
}
} else if (coordinates[0].length == 3) {
for (int i = 0; i < coordinates.length; ++i) {
s.vertex(coordinates[i][0], coordinates[i][1], coordinates[i][2]);
}
} else if (coordinates[0].length == 4) {
for (int i = 0; i < coordinates.length; ++i) {
s.vertex(coordinates[i][0], coordinates[i][1], coordinates[i][2], coordinates[i][3]);
}
} else if (coordinates[0].length == 5) {
for (int i = 0; i < coordinates.length; ++i) {
s.vertex(coordinates[i][0], coordinates[i][1], coordinates[i][2], coordinates[i][3], coordinates[i][4]);
}
} else {
throw new RuntimeException("the second axis of parameter coordinates must have a length equal to 2, 3, 4, or 5");
}
}

public static void bezierVertices(PShape s, float[][] coordinates) {
if (coordinates.length == 0) {
// no vertices
return;
}

if (coordinates[0].length == 6) {
for (int i = 0; i < coordinates.length; ++i) {
s.bezierVertex(coordinates[i][0], coordinates[i][1], coordinates[i][2], coordinates[i][3], coordinates[i][4],
coordinates[i][5]);
}
} else if (coordinates[0].length == 9) {
for (int i = 0; i < coordinates.length; ++i) {
s.bezierVertex(coordinates[i][0], coordinates[i][1], coordinates[i][2], coordinates[i][3], coordinates[i][4],
coordinates[i][5], coordinates[i][6], coordinates[i][7], coordinates[i][8]);
}
} else {
throw new RuntimeException("the second axis of parameter coordinates must have a length equal to 6 or 9");
}
}

public static void curveVertices(PShape s, float[][] coordinates) {
if (coordinates.length == 0) {
// no vertices
return;
}

if (coordinates[0].length == 2) {
for (int i = 0; i < coordinates.length; ++i) {
s.curveVertex(coordinates[i][0], coordinates[i][1]);
}
} else if (coordinates[0].length == 3) {
for (int i = 0; i < coordinates.length; ++i) {
s.curveVertex(coordinates[i][0], coordinates[i][1], coordinates[i][2]);
}
} else {
throw new RuntimeException("the second axis of parameter coordinates must have a length equal to 2 or 3");
}
}

public static void quadraticVertices(PShape s, float[][] coordinates) {
if (coordinates.length == 0) {
// no vertices
return;
}

if (coordinates[0].length == 4) {
for (int i = 0; i < coordinates.length; ++i) {
s.quadraticVertex(coordinates[i][0], coordinates[i][1], coordinates[i][2], coordinates[i][3]);
}
} else if (coordinates[0].length == 6) {
for (int i = 0; i < coordinates.length; ++i) {
s.quadraticVertex(coordinates[i][0], coordinates[i][1], coordinates[i][2], coordinates[i][3], coordinates[i][4],
coordinates[i][5]);
}
} else {
throw new RuntimeException("the second axis of parameter coordinates must have a length equal to 4 or 6");
}
}

}
18 changes: 11 additions & 7 deletions py5_resources/data/pshape.csv
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ BEZIER_VERTEX,BEZIER_VERTEX,,static field,JAVA,
QUADRATIC_VERTEX,QUADRATIC_VERTEX,,static field,JAVA,
CURVE_VERTEX,CURVE_VERTEX,,static field,JAVA,
BREAK,BREAK,,static field,JAVA,
QUAD_BEZIER_VERTEX,QUAD_BEZIER_VERTEX,,static field,SKIP,
QUAD_BEZIER_VERTEX,QUAD_BEZIER_VERTEX,,static field,JAVA,
PI,PI,,static field,SKIP,
HALF_PI,HALF_PI,,static field,SKIP,
THIRD_PI,THIRD_PI,,static field,SKIP,
Expand Down Expand Up @@ -114,11 +114,11 @@ CLAMP,CLAMP,,static field,SKIP,
REPEAT,REPEAT,,static field,SKIP,
MODEL,MODEL,,static field,SKIP,
SHAPE,SHAPE,,static field,SKIP,
SQUARE,SQUARE,,static field,SKIP,
ROUND,ROUND,,static field,SKIP,
PROJECT,PROJECT,,static field,SKIP,
MITER,MITER,,static field,SKIP,
BEVEL,BEVEL,,static field,SKIP,
SQUARE,SQUARE,,static field,JAVA,
ROUND,ROUND,,static field,JAVA,
PROJECT,PROJECT,,static field,JAVA,
MITER,MITER,,static field,JAVA,
BEVEL,BEVEL,,static field,JAVA,
AMBIENT,AMBIENT,,static field,SKIP,
DIRECTIONAL,DIRECTIONAL,,static field,SKIP,
SPOT,SPOT,,static field,SKIP,
Expand Down Expand Up @@ -317,4 +317,8 @@ rotate_y,rotateY,,method,JAVA,
rotate_z,rotateZ,,method,JAVA,
reset_matrix,resetMatrix,,method,JAVA,
apply_matrix,applyMatrix,,method,JAVA,
add_name,addName,,method,SKIP,
add_name,addName,,method,SKIP,
vertices,,,method,PYTHON,implemented by me in Py5ShapeHelper
bezier_vertices,,,method,PYTHON,implemented by me in Py5ShapeHelper
curve_vertices,,,method,PYTHON,implemented by me in Py5ShapeHelper
quadratic_vertices,,,method,PYTHON,implemented by me in Py5ShapeHelper
Loading

0 comments on commit 6d89f7e

Please sign in to comment.