-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
close #389 - 3DTilesの実装に従い、normalの計算をしました - (ちゃんと影が出ていると思います。)  <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **新機能** - 3D空間での頂点の法線を計算する機能を追加しました。 - **リファクタ** - 頂点の構造を法線ベクトルを含むように変更しました。 - 頂点配列を扱うロジックを更新しました。 <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
1 parent
62c4ee1
commit d9161e7
Showing
3 changed files
with
84 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#[inline] | ||
fn cross((ax, ay, az): (f64, f64, f64), (bx, by, bz): (f64, f64, f64)) -> (f64, f64, f64) { | ||
(ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx) | ||
} | ||
|
||
pub fn calculate_normal(vertices: &[f64], dim: usize) -> Option<(f64, f64, f64)> { | ||
let len = vertices.len(); | ||
if len < dim * 3 { | ||
// At least 3 vertices required | ||
return None; | ||
} | ||
let last_point = ( | ||
vertices[len - dim], | ||
vertices[len - dim + 1], | ||
vertices[len - dim + 2], | ||
); | ||
|
||
let (sum, _) = | ||
vertices | ||
.chunks_exact(dim) | ||
.fold(((0., 0., 0.), last_point), |(acc, prev), data| { | ||
let (x, y, z) = (data[0], data[1], data[2]); | ||
let c = cross( | ||
(prev.0 - x, prev.1 - y, prev.2 - z), | ||
(prev.0 + x, prev.1 + y, prev.2 + z), | ||
); | ||
((acc.0 + c.0, acc.1 + c.1, acc.2 + c.2), (x, y, z)) | ||
}); | ||
|
||
match (sum.0 * sum.0 + sum.1 * sum.1 + sum.2 * sum.2).sqrt() { | ||
d if d < 1e-30 => None, | ||
d => Some((sum.0 / d, sum.1 / d, sum.2 / d)), | ||
} | ||
} |