Skip to content

Commit

Permalink
refactor(Clusters, ForceLink, ForceManyBody, Lines, Points): Improve …
Browse files Browse the repository at this point in the history
…vector calculations

With the assistance of CodeRabbit 🐇
  • Loading branch information
Stukova authored and rokotyan committed Dec 20, 2024
1 parent 6db1efc commit 68906cd
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 13 deletions.
5 changes: 2 additions & 3 deletions src/modules/Clusters/force-cluster.frag
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ void main() {
}
vec4 clusterCustomCoeff = texture2D(clusterForceCoefficient, textureCoords);
vec2 distVector = clusterPositions.xy - pointPosition.xy;
float dist = sqrt(dot(distVector, distVector));
float dist = length(distVector);
if (dist > 0.0) {
float angle = atan(distVector.y, distVector.x);
float addV = alpha * dist * clusterCoefficient * clusterCustomCoeff.r;
velocity.rg += addV * vec2(cos(angle), sin(angle));
velocity.rg += addV * normalize(distVector);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/Clusters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Clusters extends CoreModule {

// Find the highest cluster index in the array and add 1 (since cluster indices start at 0).
const clusterNumber = (data.pointClusters ?? []).reduce<number>((max, clusterIndex) => {
if (clusterIndex === undefined) return max
if (clusterIndex === undefined || clusterIndex < 0) return max
return Math.max(max, clusterIndex)
}, 0) + 1

Expand Down
8 changes: 6 additions & 2 deletions src/modules/ForceLink/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ export class ForceLink extends CoreModule {
this.indices[linkIndex * 4 + 1] = Math.floor(connectedPointIndex / pointsTextureSize)
const degree = data.degree?.[connectedPointIndex] ?? 0
const connectedDegree = data.degree?.[pointIndex] ?? 0
const bias = degree / (degree + connectedDegree)
let strength = data.linkStrength?.[initialLinkIndex] ?? (1 / Math.min(degree, connectedDegree))
const degreeSum = degree + connectedDegree
// Prevent division by zero
const bias = degreeSum !== 0 ? degree / degreeSum : 0.5
const minDegree = Math.min(degree, connectedDegree)
// Prevent division by zero
let strength = data.linkStrength?.[initialLinkIndex] ?? (1 / Math.max(minDegree, 1))
strength = Math.sqrt(strength)
linkBiasAndStrengthState[linkIndex * 4 + 0] = bias
linkBiasAndStrengthState[linkIndex * 4 + 1] = strength
Expand Down
3 changes: 1 addition & 2 deletions src/modules/ForceManyBody/force-level.frag
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ vec2 calculateAdditionalVelocity (vec2 ij, vec2 pp) {
float l = dot(distVector, distVector);
float dist = sqrt(l);
if (l > 0.0) {
float angle = atan(distVector.y, distVector.x);
float c = alpha * repulsion * centermass.b;

float distanceMin2 = 1.0;
if (l < distanceMin2) l = sqrt(distanceMin2 * l);
float addV = c / sqrt(l);
add = addV * vec2(cos(angle), sin(angle));
add = addV * normalize(distVector);
}
}
return add;
Expand Down
9 changes: 6 additions & 3 deletions src/modules/Lines/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,19 @@ export class Lines extends CoreModule {
public updatePointsBuffer (): void {
const { reglInstance, data, store } = this
if (data.linksNumber === undefined || data.links === undefined) return
const instancePoints: [number, number][] = [] // new Float32Array(data.linksNumber * 2)
const instancePoints = new Float32Array(data.linksNumber * 4)
for (let i = 0; i < data.linksNumber; i++) {
const fromIndex = data.links[i * 2] as number
const toIndex = data.links[i * 2 + 1] as number
const fromX = fromIndex % store.pointsTextureSize
const fromY = Math.floor(fromIndex / store.pointsTextureSize)
const toX = toIndex % store.pointsTextureSize
const toY = Math.floor(toIndex / store.pointsTextureSize)
instancePoints[i * 2] = [fromX, fromY]
instancePoints[i * 2 + 1] = [toX, toY]
const offset = i * 4
instancePoints[offset] = fromX
instancePoints[offset + 1] = fromY
instancePoints[offset + 2] = toX
instancePoints[offset + 3] = toY
}

if (!this.pointsBuffer) this.pointsBuffer = reglInstance.buffer(0)
Expand Down
5 changes: 3 additions & 2 deletions src/modules/Points/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import regl from 'regl'
// import { scaleLinear } from 'd3-scale'
// import { extent } from 'd3-array'
import { CoreModule } from '@/graph/modules/core-module'
// import { defaultConfigValues } from '@/graph/variables'
import { defaultConfigValues } from '@/graph/variables'
import drawPointsFrag from '@/graph/modules/Points/draw-points.frag'
import drawPointsVert from '@/graph/modules/Points/draw-points.vert'
import findPointsOnAreaSelectionFrag from '@/graph/modules/Points/find-points-on-area-selection.frag'
Expand Down Expand Up @@ -465,7 +465,8 @@ export class Points extends CoreModule {

public updateSampledPointsGrid (): void {
const { store: { screenSize }, config: { pointSamplingDistance }, reglInstance } = this
const dist = pointSamplingDistance ?? Math.min(...screenSize) / 2
let dist = pointSamplingDistance ?? Math.min(...screenSize) / 2
if (dist === 0) dist = defaultConfigValues.pointSamplingDistance
const w = Math.ceil(screenSize[0] / dist)
const h = Math.ceil(screenSize[1] / dist)
destroyFramebuffer(this.sampledPointsFbo)
Expand Down

0 comments on commit 68906cd

Please sign in to comment.