Skip to content

Commit 4bc8ab2

Browse files
committed
ported updates & fixes from java mode
1 parent 28e7480 commit 4bc8ab2

File tree

12 files changed

+690
-583
lines changed

12 files changed

+690
-583
lines changed

core/src/assets/shaders/LineVert.glsl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ void main() {
7171
// and corrects for aspect ratio, see https://github.com/processing/processing/issues/5181)
7272
// t = +- normalize( (q.xy*p.w - p.xy*q.w) * viewport.zw )
7373

74-
vec2 tangent = normalize((q.xy*p.w - p.xy*q.w) * viewport.zw);
74+
vec2 tangent = (q.xy*p.w - p.xy*q.w) * viewport.zw;
75+
// don't normalize zero vector (line join triangles and lines perpendicular to the eye plane)
76+
tangent = length(tangent) == 0.0 ? vec2(0.0, 0.0) : normalize(tangent);
7577

7678
// flip tangent to normal (it's already normalized)
7779
vec2 normal = vec2(-tangent.y, tangent.x);

core/src/processing/core/PGraphics.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,11 +2505,10 @@ public void sphere(float r) {
25052505
* endShape();</PRE>
25062506
*/
25072507
public float bezierPoint(float a, float b, float c, float d, float t) {
2508-
float t1 = 1.0f - t;
2509-
return a*t1*t1*t1 + 3*b*t*t1*t1 + 3*c*t*t*t1 + d*t*t*t;
2508+
float t1 = t-1.0f;
2509+
return t * ( 3*t1*(b*t1-c*t) + d*t*t ) - a*t1*t1*t1;
25102510
}
25112511

2512-
25132512
/**
25142513
* Provide the tangent at the given point on the bezier curve.
25152514
* Fix from davbol for 0136.

core/src/processing/data/FloatDict.java

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2-
3-
/*
4-
Part of the Processing project - http://processing.org
5-
6-
Copyright (c) 2013-16 The Processing Foundation
7-
8-
This library is free software; you can redistribute it and/or
9-
modify it under the terms of the GNU Lesser General Public
10-
License as published by the Free Software Foundation, version 2.
11-
12-
This library is distributed in the hope that it will be useful,
13-
but WITHOUT ANY WARRANTY; without even the implied warranty
14-
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15-
See the GNU Lesser General Public License for more details.
16-
17-
You should have received a copy of the GNU Lesser General
18-
Public License along with this library; if not, write to the
19-
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20-
Boston, MA 02111-1307 USA
21-
*/
22-
231
package processing.data;
242

253
import java.io.*;
@@ -45,7 +23,7 @@ public class FloatDict {
4523
protected float[] values;
4624

4725
/** Internal implementation for faster lookups */
48-
private HashMap<String, Integer> indices = new HashMap<String, Integer>();
26+
private HashMap<String, Integer> indices = new HashMap<>();
4927

5028

5129
public FloatDict() {
@@ -137,6 +115,31 @@ public int size() {
137115
}
138116

139117

118+
/**
119+
* Resize the internal data, this can only be used to shrink the list.
120+
* Helpful for situations like sorting and then grabbing the top 50 entries.
121+
*/
122+
public void resize(int length) {
123+
if (length == count) return;
124+
125+
if (length > count) {
126+
throw new IllegalArgumentException("resize() can only be used to shrink the dictionary");
127+
}
128+
if (length < 1) {
129+
throw new IllegalArgumentException("resize(" + length + ") is too small, use 1 or higher");
130+
}
131+
132+
String[] newKeys = new String[length];
133+
float[] newValues = new float[length];
134+
PApplet.arrayCopy(keys, newKeys, length);
135+
PApplet.arrayCopy(values, newValues, length);
136+
keys = newKeys;
137+
values = newValues;
138+
count = length;
139+
resetIndices();
140+
}
141+
142+
140143
/**
141144
* Remove all entries.
142145
*
@@ -145,7 +148,15 @@ public int size() {
145148
*/
146149
public void clear() {
147150
count = 0;
148-
indices = new HashMap<String, Integer>();
151+
indices = new HashMap<>();
152+
}
153+
154+
155+
private void resetIndices() {
156+
indices = new HashMap<>(count);
157+
for (int i = 0; i < count; i++) {
158+
indices.put(keys[i], i);
159+
}
149160
}
150161

151162

@@ -183,8 +194,8 @@ public void remove() {
183194
}
184195

185196
public Entry next() {
197+
++index;
186198
Entry e = new Entry(keys[index], values[index]);
187-
index++;
188199
return e;
189200
}
190201

@@ -368,6 +379,15 @@ public void set(String key, float amount) {
368379
}
369380

370381

382+
public void setIndex(int index, String key, float value) {
383+
if (index < 0 || index >= count) {
384+
throw new ArrayIndexOutOfBoundsException(index);
385+
}
386+
keys[index] = key;
387+
values[index] = value;
388+
}
389+
390+
371391
/**
372392
* @webref floatdict:method
373393
* @brief Check if a key is a part of the data structure
@@ -427,7 +447,7 @@ public void div(String key, float amount) {
427447
private void checkMinMax(String functionName) {
428448
if (count == 0) {
429449
String msg =
430-
String.format("Cannot use %s() on an empty %s.",
450+
String.format("Cannot use %s() on an empty %s.",
431451
functionName, getClass().getSimpleName());
432452
throw new RuntimeException(msg);
433453
}
@@ -544,6 +564,27 @@ public float maxValue() {
544564
}
545565

546566

567+
public float sum() {
568+
double amount = sumDouble();
569+
if (amount > Float.MAX_VALUE) {
570+
throw new RuntimeException("sum() exceeds " + Float.MAX_VALUE + ", use sumDouble()");
571+
}
572+
if (amount < -Float.MAX_VALUE) {
573+
throw new RuntimeException("sum() lower than " + -Float.MAX_VALUE + ", use sumDouble()");
574+
}
575+
return (float) amount;
576+
}
577+
578+
579+
public double sumDouble() {
580+
double sum = 0;
581+
for (int i = 0; i < count; i++) {
582+
sum += values[i];
583+
}
584+
return sum;
585+
}
586+
587+
547588
public int index(String what) {
548589
Integer found = indices.get(what);
549590
return (found == null) ? -1 : found.intValue();
@@ -717,10 +758,7 @@ public void swap(int a, int b) {
717758
s.run();
718759

719760
// Set the indices after sort/swaps (performance fix 160411)
720-
indices = new HashMap<String, Integer>();
721-
for (int i = 0; i < count; i++) {
722-
indices.put(keys[i], i);
723-
}
761+
resetIndices();
724762
}
725763

726764

@@ -730,10 +768,7 @@ public void swap(int a, int b) {
730768
* @return a FloatDict with the original keys, mapped to their pct of the total
731769
*/
732770
public FloatDict getPercent() {
733-
double sum = 0;
734-
for (int i = 0; i < count; i++) {
735-
sum += values[i];
736-
}
771+
double sum = sum();
737772
FloatDict outgoing = new FloatDict();
738773
for (int i = 0; i < size(); i++) {
739774
double percent = value(i) / sum;
@@ -791,4 +826,4 @@ public String toJSON() {
791826
public String toString() {
792827
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
793828
}
794-
}
829+
}

core/src/processing/data/IntDict.java

Lines changed: 69 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2-
3-
/*
4-
Part of the Processing project - http://processing.org
5-
6-
Copyright (c) 2013-16 The Processing Foundation
7-
8-
This library is free software; you can redistribute it and/or
9-
modify it under the terms of the GNU Lesser General Public
10-
License as published by the Free Software Foundation, version 2.
11-
12-
This library is distributed in the hope that it will be useful,
13-
but WITHOUT ANY WARRANTY; without even the implied warranty
14-
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15-
See the GNU Lesser General Public License for more details.
16-
17-
You should have received a copy of the GNU Lesser General
18-
Public License along with this library; if not, write to the
19-
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20-
Boston, MA 02111-1307 USA
21-
*/
22-
231
package processing.data;
242

253
import java.io.*;
@@ -45,7 +23,7 @@ public class IntDict {
4523
protected int[] values;
4624

4725
/** Internal implementation for faster lookups */
48-
private HashMap<String, Integer> indices = new HashMap<String, Integer>();
26+
private HashMap<String, Integer> indices = new HashMap<>();
4927

5028

5129
public IntDict() {
@@ -138,6 +116,29 @@ public int size() {
138116
}
139117

140118

119+
/**
120+
* Resize the internal data, this can only be used to shrink the list.
121+
* Helpful for situations like sorting and then grabbing the top 50 entries.
122+
*/
123+
public void resize(int length) {
124+
if (length > count) {
125+
throw new IllegalArgumentException("resize() can only be used to shrink the dictionary");
126+
}
127+
if (length < 1) {
128+
throw new IllegalArgumentException("resize(" + length + ") is too small, use 1 or higher");
129+
}
130+
131+
String[] newKeys = new String[length];
132+
int[] newValues = new int[length];
133+
PApplet.arrayCopy(keys, newKeys, length);
134+
PApplet.arrayCopy(values, newValues, length);
135+
keys = newKeys;
136+
values = newValues;
137+
count = length;
138+
resetIndices();
139+
}
140+
141+
141142
/**
142143
* Remove all entries.
143144
*
@@ -146,7 +147,15 @@ public int size() {
146147
*/
147148
public void clear() {
148149
count = 0;
149-
indices = new HashMap<String, Integer>();
150+
indices = new HashMap<>();
151+
}
152+
153+
154+
private void resetIndices() {
155+
indices = new HashMap<>(count);
156+
for (int i = 0; i < count; i++) {
157+
indices.put(keys[i], i);
158+
}
150159
}
151160

152161

@@ -184,8 +193,8 @@ public void remove() {
184193
}
185194

186195
public Entry next() {
196+
++index;
187197
Entry e = new Entry(keys[index], values[index]);
188-
index++;
189198
return e;
190199
}
191200

@@ -370,6 +379,16 @@ public void set(String key, int amount) {
370379
}
371380
}
372381

382+
383+
public void setIndex(int index, String key, int value) {
384+
if (index < 0 || index >= count) {
385+
throw new ArrayIndexOutOfBoundsException(index);
386+
}
387+
keys[index] = key;
388+
values[index] = value;
389+
}
390+
391+
373392
/**
374393
* @webref intdict:method
375394
* @brief Check if a key is a part of the data structure
@@ -452,7 +471,7 @@ public void div(String key, int amount) {
452471
private void checkMinMax(String functionName) {
453472
if (count == 0) {
454473
String msg =
455-
String.format("Cannot use %s() on an empty %s.",
474+
String.format("Cannot use %s() on an empty %s.",
456475
functionName, getClass().getSimpleName());
457476
throw new RuntimeException(msg);
458477
}
@@ -530,6 +549,27 @@ public int maxValue() {
530549
}
531550

532551

552+
public int sum() {
553+
long amount = sumLong();
554+
if (amount > Integer.MAX_VALUE) {
555+
throw new RuntimeException("sum() exceeds " + Integer.MAX_VALUE + ", use sumLong()");
556+
}
557+
if (amount < Integer.MIN_VALUE) {
558+
throw new RuntimeException("sum() less than " + Integer.MIN_VALUE + ", use sumLong()");
559+
}
560+
return (int) amount;
561+
}
562+
563+
564+
public long sumLong() {
565+
long sum = 0;
566+
for (int i = 0; i < count; i++) {
567+
sum += values[i];
568+
}
569+
return sum;
570+
}
571+
572+
533573
public int index(String what) {
534574
Integer found = indices.get(what);
535575
return (found == null) ? -1 : found.intValue();
@@ -685,10 +725,7 @@ public void swap(int a, int b) {
685725
s.run();
686726

687727
// Set the indices after sort/swaps (performance fix 160411)
688-
indices = new HashMap<String, Integer>();
689-
for (int i = 0; i < count; i++) {
690-
indices.put(keys[i], i);
691-
}
728+
resetIndices();
692729
}
693730

694731

@@ -698,10 +735,7 @@ public void swap(int a, int b) {
698735
* @return an IntDict with the original keys, mapped to their pct of the total
699736
*/
700737
public FloatDict getPercent() {
701-
double sum = 0;
702-
for (int i = 0; i < count; i++) {
703-
sum += values[i];
704-
}
738+
double sum = sum(); // a little more accuracy
705739
FloatDict outgoing = new FloatDict();
706740
for (int i = 0; i < size(); i++) {
707741
double percent = value(i) / sum;
@@ -759,4 +793,4 @@ public String toJSON() {
759793
public String toString() {
760794
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
761795
}
762-
}
796+
}

0 commit comments

Comments
 (0)