Sample

Schnappinger/aoi/artofillusion.math.Cells

Human rating uses the original dataset scale.

Human2.42
Lines267

Scores

MethodScoreScaleDataset rank
(0–100)
Posnett00–1; higher is more readable
17.4Δ 4.5
Scalabrino0.8140–1; higher is more readable
66.4Δ 53.6
Dorn0.0510–1; higher is more readable
28.7Δ 15.8
Mi0.5980–1; higher is more readable
62.0Δ 49.2
LLOC144Logical lines; fewer is more readable
18.2Δ 5.3
DeepSeek V4 Pro140–20; mean of three runs
29.9Δ 17.0
GPT-6.1 Sol130–20; mean of three runs
9.4Δ 3.5
BTTF0.1430–1; higher is more readable
5.9Δ 6.9
Human2.421–4; original rating
12.9

Views

Source code

/* Copyright (C) 2001-2005 by Peter Eastman This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. */ package artofillusion.math; import java.util.*; /** This class provides an implementation of Steven Worley's cellular texture basis function as described in <p> Worley, S. "A Cellular Texture Basis Function." Siggraph Proceedings, pp. 291-294, 1996. <p> This function scatters "feature points" at random locations throughout 3D space. At any point (x, y, z), it defines the function f1(x, y, z) to be the distance to the nearest feature point. It similarly defines f2(x, y, z) as the distance to the second nearest feature point, and similarly for f3(x, y, z), etc. */ public class Cells { private double dx[], dy[], dz[]; private Vec3 feature, diff; private Random random = new FastRandom(0); private int metric; private static double prob[]; public static final int EUCLIDEAN = 0; public static final int CITY_BLOCK = 1; public static final int CHESS_BOARD = 2; private static final double DENSITY = 3.0; private static final int cellIndex[][] = new int [][] {{0, 1, 1}, {2, 1, 1}, {1, 0, 1}, {1, 2, 1}, {1, 1, 0}, {1, 1, 2}, {0, 0, 1}, {0, 2, 1}, {0, 1, 0}, {0, 1, 2}, {2, 0, 1}, {2, 2, 1}, {2, 1, 0}, {2, 1, 2}, {1, 0, 0}, {1, 0, 2}, {1, 2, 0}, {1, 2, 2}, {0, 0, 0}, {0, 0, 2}, {0, 2, 0}, {0, 2, 2}, {2, 0, 0}, {2, 0, 2}, {2, 2, 0}, {2, 2, 2}}; static { // Calculate the probabilities for different numbers of points to be in a cell. prob = new double [10]; for (int i = 0; i < 10; i++) { prob[i] = FastMath.pow(DENSITY, i) * Math.exp(-DENSITY); for (int j = 2; j <= i; j++) prob[i] /= (double) j; } for (int i = 1; i < 10; i++) prob[i] += prob[i-1]; } public Cells() { dx = new double [3]; dy = new double [3]; dz = new double [3]; feature = new Vec3(); diff = new Vec3(); metric = EUCLIDEAN; } /** Get which distance metric is being used. Allowed values are EUCLIDEAN, CITY_BLOCK, or CHESS_BOARD. */ public int getMetric() { return metric; } /** Set which distance metric to use. Allowed values are EUCLIDEAN, CITY_BLOCK, or CHESS_BOARD. */ public void setMetric(int metric) { this.metric = metric; } /** Calculate the various functions at a specified point. The number of functions to evaluate is determined by the length of value[]. The values of the functions are returned in value[], and their gradients in grad[]. A "unique identifier" for the nearest, second nearest, etc. feature point is returned in id. If gradients are not needed, null may be passed for grad[]. */ public void calcFunctions(Vec3 p, double value[], Vec3 grad[], int id[]) { int i, j, k, m, num; int a, b, c; int seed; double x, y, z, r, dist2, rand; a = floor(p.x); b = floor(p.y); c = floor(p.z); x = p.x - (double) a; y = p.y - (double) b; z = p.z - (double) c; for (i = 0; i < value.length; i++) value[i] = Double.MAX_VALUE; // Find the number of features in this cell. seed = randSeed(a, b, c); random.setSeed(seed); random.nextInt(); rand = random.nextDouble(); for (num = 1; num < 9 && rand > prob[num]; num++); // Find the nearest features in this cell. for (i = 0; i < num; i++) { feature.x = random.nextDouble(); feature.y = random.nextDouble(); feature.z = random.nextDouble(); diff.x = x-feature.x; diff.y = y-feature.y; diff.z = z-feature.z; dist2 = distance(diff); sortIntoList(dist2, seed+i, value, grad, id); } // Check feature points in all the surrounding cells. dx[0] = x*x; dy[0] = y*y; dz[0] = z*z; dx[2] = (1.0-x)*(1.0-x); dy[2] = (1.0-y)*(1.0-y); dz[2] = (1.0-z)*(1.0-z); r = value[value.length-1]; for (int cell = 0; cell < cellIndex.length; cell++) { i = cellIndex[cell][0]; j = cellIndex[cell][1]; k = cellIndex[cell][2]; if (r < dx[i]+dy[j]+dz[k]) continue; i--; j--; k--; seed = randSeed(a+i, b+j, c+k); random.setSeed(seed); random.nextInt(); rand = random.nextDouble(); for (num = 1; num < 9 && rand > prob[num]; num++); // Find the nearest features in this cell. for (m = 0; m < num; m++) { feature.x = random.nextDouble() + (double) i; feature.y = random.nextDouble() + (double) j; feature.z = random.nextDouble() + (double) k; diff.x = x-feature.x; diff.y = y-feature.y; diff.z = z-feature.z; dist2 = distance(diff); if (dist2 < value[value.length-1]) sortIntoList(dist2, seed+m, value, grad, id); } r = value[value.length-1]; } if (metric == EUCLIDEAN) for (i = 0; i < value.length; i++) { value[i] = Math.sqrt(value[i]); if (grad != null) grad[i].scale(1.0/value[i]); } } /** Calculate the random number seed to use for cell (i, j, k). */ private static final int randSeed(int i, int j, int k) { return i*10 + j*1000 + k*100000; } /** Given the squared distance to a feature point, sort that point into the list. */ private final void sortIntoList(double dist2, int newid, double value[], Vec3 grad[], int id[]) { for (int i = 0; i < value.length; i++) if (dist2 < value[i]) { for (int j = value.length-1; j > i; j--) { value[j] = value[j-1]; id[j] = id[j-1]; if (grad != null) grad[j].set(grad[j-1]); } value[i] = dist2; id[i] = newid; if (grad != null) { if (metric == EUCLIDEAN) grad[i].set(diff); else if (metric == CITY_BLOCK) grad[i].set(diff.x > 0.0 ? 1.0 : -1.0, diff.y > 0.0 ? 1.0 : -1.0, diff.z > 0.0 ? 1.0 : -1.0); else { double ax = Math.abs(diff.x); double ay = Math.abs(diff.y); double az = Math.abs(diff.z); if (ax > ay) { if (ax > az) grad[i].set(diff.x > 0.0 ? 1.0 : -1.0, 0.0, 0.0); else grad[i].set(0.0, 0.0, diff.z > 0.0 ? 1.0 : -1.0); } else { if (ay > az) grad[i].set(0.0, diff.y > 0.0 ? 1.0 : -1.0, 0.0); else grad[i].set(0.0, 0.0, diff.z > 0.0 ? 1.0 : -1.0); } } } return; } } /** Given the vector between two points, calculate the distance measure for the desired metric. */ private final double distance(Vec3 diff) { if (metric == EUCLIDEAN) return diff.length2(); double ax = Math.abs(diff.x); double ay = Math.abs(diff.y); double az = Math.abs(diff.z); if (metric == CITY_BLOCK) return ax+ay+az; if (ax > ay) return (ax > az ? ax : az); return (ay > az ? ay : az); } /** A faster replacement for Math.floor(). */ private static int floor(double d) { if (d < 0.0) { int f = (int) d; if (f != d) f -= 1; return f; } return (int) d; } }

Loading feature diagnosis…

Source regions

Feature contributions

Feature contributions

FeatureValueContribution
(log-odds)
Halstead volumeHow much symbolic information the code contains. It combines the total number of operators and values with the number of distinct ones.12120.465−399.975
Source linesThe number of non-empty source lines.267+106.8
Byte entropyHow varied the bytes in the source text are. Repeated bytes give a lower value; a more even mixture gives a higher value.4.631−6.947

Intercept 8.87 + feature contributions = -291.252 (log-odds).
Score = sigmoid(-291.252) = 0.

Score0.814

Feature contributions

FeatureValueContribution
(log-odds)
Comment layout frequencyHow frequently the layout switches between lines containing only comments or whitespace and lines containing code.268−8.453
Indentation frequencyHow rapidly indentation changes from line to line.236+7.426
Number layout frequencyHow rapidly the number of numeric values per line changes through the code.236−4.995
Identifier layout (vertical)How rapidly the visual pattern of identifier characters changes from top to bottom.266.855+3.32
Comment layout (horizontal)How rapidly the visual pattern of comment characters changes from left to right.108.966+0.954
Identifier layout (horizontal)How rapidly the visual pattern of identifier characters changes from left to right. It measures spatial frequency, not the meaning of the names.108.974−0.951
Operator visual areaThe share of the code’s visual area occupied by operators such as +, =, and *.0.128−0.304

Intercept 0.07 + feature contributions = -2.932 (log-odds).
Score = sigmoid(-2.932) = 0.051.

Score0.598
Score144
Run 1

The code demonstrates good overall structure with logical organization into methods, clear class-level constants, and descriptive naming. The main algorithm flows naturally from local to surrounding cell processing. However, several areas reduce readability: the calcFunctions method is long and contains multiple responsibilities; the sortIntoList method has deeply nested conditionals (especially for gradient calculation); and some complex expressions like the CHESS_BOARD metric logic are dense. While variable names are generally clear, the cellIndex array initialization is visually cluttered. The code benefits from good comments explaining the algorithm and static probability initialization, but the core distance/sorting logic could use further decomposition. Nesting depth in the gradient calculation reaches four levels, making intent harder to follow. Overall, readable but with room for refactoring the most complex sections.

Run 2

The code demonstrates good structure with clear method separation and documentation. The main algorithm is relatively flat but suffers from deep nesting in methods like sortIntoList. Variable names are generally clear (e.g., 'dist2', 'feature', 'diff'), and the mathematical intent is documented with references. However, some sections are verbose—particularly the repeated cell-index checking and metric-specific gradient calculations. Action granularity is mostly one action per line, though multiple array operations occur in single lines. The reading flow is logical but occasionally interrupted by long, dense blocks (e.g., the nested ternary expressions). The cellIndex initialization is hard to parse. Overall, it is readable for domain experts but could benefit from extracting some complex conditionals into helper methods or constants.

Run 3

The code has some positive aspects: clear class-level documentation, logical separation of methods, and mostly descriptive variable names. However, several issues reduce readability. The calcFunctions method is quite long and contains deeply nested loops and conditionals (up to 4 levels in some places), violating flat nesting preferences. Intent is somewhat clear from comments, but the core algorithm's flow is complex and hard to follow without domain knowledge. Multiple actions appear on single lines (e.g., 'i--; j--; k--;' on one line, or multiple assignments in one expression). The sortIntoList method contains deeply nested ternary-like logic and multi-branch gradient calculations that are hard to parse quickly. Magic numbers (e.g., 9 in loops, 10 for prob array) appear without named constants. The overall structure is functional, but the dense implementation, mixed granularity, and mathematical complexity push it toward the less readable end of the scale.

Run 1

Code Structure: The class separates initialization, evaluation, distance calculation, and insertion into helper methods, but calcFunctions handles several responsibilities and helpers rely on shared mutable state. Nesting: Most evaluation logic is moderately nested, while sortIntoList has deeply nested gradient-selection branches. Clarity of intent: Strong class documentation and phase comments explain the algorithm, but abbreviated variables, unexplained seed constants, and empty-body loops impede understanding. The dist2 name is misleading for non-Euclidean metrics. Code length: Reasonable for the algorithm, though duplicated feature-generation logic and gradient branches could be extracted or simplified. Action granularity: Most statements perform one action per line; grouped declarations and dense ternary expressions reduce clarity slightly. Reading flow: Methods and comments provide a logical progression, but inconsistent indentation, omitted braces, implicit dependence on diff, and repeated index manipulation require careful tracking.

Run 2

Code Structure: The class is logically organized into initialization, public methods, and focused helpers, but calcFunctions combines several responsibilities and relies on mutable shared state. Nesting: Most traversal is moderate, but sortIntoList has deeply nested loops and metric-dependent conditionals. Clarity of intent: Class documentation and phase comments explain the algorithm well; terse variable names, unexplained constants, empty-body loops, and implicit use of diff obscure implementation details. Code length: Reasonable for the algorithm, though repeated feature-generation logic and verbose gradient selection could be extracted into helpers. Action granularity: Statements generally perform one action per line, but grouped declarations and dense ternary expressions reduce scanability. Reading flow: Processing follows a natural sequence from cell setup through neighbor search to gradient normalization, but inconsistent indentation, omitted braces, and tightly spaced arithmetic interrupt the flow.

Run 3

Code Structure: A cohesive class with useful helper methods, but feature generation is duplicated and gradient calculation is embedded in list insertion. Nesting: Most methods are manageable, while sortIntoList contains deeply nested loops and metric-dependent branches. Clarity of intent: Class documentation, method comments, and named metric constants explain the algorithm well; terse variables, magic numbers, implicit shared state, and empty-body loops obscure details. Code length: Reasonable for the algorithm, but repeated cell-processing logic and verbose gradient branching could be extracted or simplified. Action granularity: Statements generally perform one action per line, although grouped declarations and dense ternary expressions reduce clarity. Reading flow: Processing proceeds logically from initialization through local and neighboring cells to normalization, supported by section comments; inconsistent spacing, omitted braces, and dense numeric tables interrupt readability.