Skip to content

Commit cdf166e

Browse files
committed
initial commit
0 parents  commit cdf166e

21 files changed

+4562
-0
lines changed

.gitignore

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Dependency directories
2+
node_modules/
3+
4+
# TypeScript compilation output
5+
dist/
6+
build/
7+
8+
# Logs
9+
logs
10+
*.log
11+
npm-debug.log*
12+
13+
14+
# Runtime data
15+
pids
16+
*.pid
17+
*.seed
18+
*.pid.lock
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Misc
24+
.DS_Store
25+
.env
26+
.env.local
27+
.env.development.local
28+
.env.test.local
29+
.env.production.local
30+
31+
# Temporary files
32+
*.tmp
33+
*.temp
34+
35+
# TypeScript cache
36+
*.tsbuildinfo
37+
38+
# Optional npm cache directory
39+
.npm
40+
41+
# Optional eslint cache
42+
.eslintcache
43+
44+
# Optional REPL history
45+
.node_repl_history
46+
47+
# Output of 'npm pack'
48+
*.tgz
49+
50+
# dotenv environment variables file
51+
.env
52+
53+
54+
55+
56+
57+
58+
59+
60+

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# b-spline-algorithms
2+
3+
A comprehensive TypeScript library for B-spline and NURBS algorithms, offering both functional and optimized implementations.
4+
5+
## Features
6+
7+
- Implements core B-spline algorithms including De Boor's algorithm, knot insertion, and more
8+
- Provides both functional (readable) and optimized (performant) versions of each algorithm
9+
- Comprehensive test suite ensuring correctness and performance
10+
- Written in TypeScript with full type definitions
11+
- Thoroughly documented with explanations of underlying mathematical concepts
12+
13+
## Installation
14+
15+
```bash
16+
npm install b-spline-algorithms
17+
```
18+
19+
## Usage
20+
21+
Basic usage example:
22+
23+
```typescript
24+
import { deBoor } from "b-spline-algorithms";
25+
26+
const controlPoints = [
27+
[0, 0],
28+
[1, 1],
29+
[2, 0],
30+
[3, 1],
31+
];
32+
const knots = [0, 0, 0, 0, 1, 1, 1, 1];
33+
const t = 0.5;
34+
const degree = 3;
35+
36+
const point = deBoor(controlPoints, knots, t, degree);
37+
console.log(point); // Output: [1.5, 0.75]
38+
```
39+
40+
For debugging or educational purposes, you can use the functional version:
41+
42+
```typescript
43+
import { deBoorDebug } from "b-spline-algorithms";
44+
45+
const point = deBoorDebug(controlPoints, knots, t, degree);
46+
```
47+
48+
## API Reference
49+
50+
See our API documentation for detailed information on all available functions.
51+
52+
## Algorithm Explanations
53+
54+
For in-depth explanations of the implemented algorithms, check our algorithm guide.
55+
56+
## Performance
57+
58+
Our optimized implementations are designed for high performance. For benchmarks and performance tips, see our performance guide.
59+
60+
## Contributing
61+
62+
We welcome contributions! Please see our contributing guidelines for details on how to get started.
63+
64+
## License
65+
66+
This project is licensed under the MIT License - see the LICENSE file for details.
67+
68+
## Acknowledgements
69+
70+
List any references, papers, or other libraries that inspired or informed your implementations
71+
72+
## Contact
73+
74+
For questions and feedback, please open an issue on our GitHub repository.

docs/algorithm-explanations.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# De Boor's Algorithm
2+
3+
## Overview
4+
5+
De Boor's algorithm is a method for evaluating points on a B-spline or NURBS curve. It's a generalization of the de Casteljau algorithm used for Bézier curves. The algorithm recursively computes a point on the B-spline curve for a given parameter value.
6+
7+
## Implementation
8+
9+
Our implementation in `src/core/functional/deBoor.ts` prioritizes readability and understanding over performance. Here's a breakdown of the algorithm:
10+
11+
### Main Function: `deBoor`
12+
13+
```typescript
14+
export function deBoor(
15+
controlPoints: Point[],
16+
knots: number[],
17+
t: number,
18+
degree: number
19+
): Point {
20+
// ...
21+
}
22+
```
23+
24+
This function takes four parameters:
25+
26+
- controlPoints: An array of control points that define the shape of the B-spline curve.
27+
28+
- knots: An array of knot values that determine how the control points affect the curve.
29+
30+
- t: The parameter value at which to evaluate the curve.
31+
32+
- degree: The degree of the B-spline curve.
33+
34+
The function performs these steps:
35+
36+
1. Finds the knot span index for the given parameter t.
37+
38+
2. Extracts the relevant control points.
39+
40+
3. Calls the recursive De Boor function to compute the final point.
41+
42+
```typescript
43+
Recursive Function: deBoorRecursive
44+
function deBoorRecursive(
45+
points: Point[],
46+
knots: number[],
47+
t: number,
48+
degree: number,
49+
spanIndex: number,
50+
r: number
51+
): Point {
52+
// ...
53+
}
54+
```
55+
56+
This function implements the core of de Boor's algorithm. It recursively computes new sets of points, each set getting closer to the final point on the curve. The recursion continues until we reach the degree of the curve.
57+
58+
Parameters:
59+
60+
- points: The current set of points being processed.
61+
62+
- knots, t, degree: Same as in the main function.
63+
64+
- spanIndex: The index of the knot span containing t.
65+
66+
- r: The current recursion depth.
67+
68+
## Helper Functions
69+
70+
1. findKnotSpanIndex: Determines which knot span contains the parameter t.
71+
72+
2. computeAlpha: Calculates the interpolation factor between two knots.
73+
74+
3. interpolate: Performs linear interpolation between two points.
75+
76+
## Mathematical Explanation
77+
78+
De Boor's algorithm is based on the recursive definition of B-spline basis functions. For a B-spline of degree p, we start with p+1 control points and recursively compute new points using linear interpolation.
79+
80+
The key formula is:
81+
82+
$$ d[i,r](t) = (1 - α) _ d[i-1,r-1](t) + α _ d[i,r-1](t) $$
83+
84+
$d[i,r]$ is the i-th point at recursion level r
85+
86+
$$α = (t - knot[i]) / (knot[i+p-r+1] - knot[i])$$
87+
88+
This process is repeated p times, resulting in the final point on the curve.
89+
90+
Advantages of This Implementation
91+
92+
- Readability : The recursive approach closely mirrors the mathematical definition, making it easier to understand the algorithm's logic.
93+
94+
- Modularity : Helper functions break down the algorithm into clear, manageable steps.
95+
96+
- Educational Value : This implementation serves as an excellent reference for learning and teaching the algorithm.
97+
98+
## Performance Considerations
99+
100+
While this implementation prioritizes clarity, it's not optimized for performance. For high-performance applications, consider using the optimized version in src/core/optimized/deBoor.ts, which uses techniques like in-place computation and loop unrolling to improve efficiency.
101+
102+
## Key optimizations in this version include:
103+
104+
1. In-place computation : Instead of creating new arrays in each iteration, we use a single working array ( points) and modify it in-place.
105+
106+
2. Loop unrolling : We've unrolled the inner loop for the 2D point calculation, avoiding the need for a separate interpolation function.
107+
108+
3. Efficient knot span search : The knot span index finding is optimized with a simple while loop, and it handles the edge case where t is at the end of the knot vector.
109+
110+
4. Minimized function calls : Helper functions have been inlined to reduce function call overhead.
111+
112+
5. Reduced memory allocation : We only allocate one array ( points) instead of creating new arrays in each recursive step.
113+
114+
6. Direct array access : We use direct array indexing instead of array methods like slice().

docs/api-reference.md

Whitespace-only changes.

docs/performance-guide.md

Whitespace-only changes.

examples/advanced-techniques.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"use strict";

examples/advanced-techniques.ts

Whitespace-only changes.

examples/basic-usage.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"use strict";

examples/basic-usage.ts

Whitespace-only changes.

jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} **/
2+
module.exports = {
3+
testEnvironment: "node",
4+
transform: {
5+
"^.+.tsx?$": ["ts-jest",{}],
6+
},
7+
};

0 commit comments

Comments
 (0)