From 22bea5ee99ae92dec4b2ed0c53bbb71672da5718 Mon Sep 17 00:00:00 2001 From: ChenX Date: Thu, 22 Mar 2018 11:42:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20Matrix2=20=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98=20=E7=AE=80=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81.=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __test__/Geometry/GeUtils.test.ts | 20 +++++++++++++++++-- .../__snapshots__/GeUtils.test.ts.snap | 16 +++++++++++++++ src/Common/CurveUtils.ts | 6 +++--- src/Geometry/Matrix2.ts | 13 +++++++----- src/Geometry/RotateUV.ts | 4 ++-- 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/__test__/Geometry/GeUtils.test.ts b/__test__/Geometry/GeUtils.test.ts index 92935542d..3137c7764 100644 --- a/__test__/Geometry/GeUtils.test.ts +++ b/__test__/Geometry/GeUtils.test.ts @@ -1,5 +1,8 @@ -import { getLoocAtUpVec, cZAxis } from "./../../src/Geometry/GeUtils" -import { Vector3 } from "three"; +import { Vector3 } from 'three'; + +import { getLoocAtUpVec } from './../../src/Geometry/GeUtils'; + +import { rotateLine } from "./../../src/Common/CurveUtils"; test('getLoocAtUpVec', () => { @@ -14,3 +17,16 @@ test('getLoocAtUpVec', () => //前视图 expect(getLoocAtUpVec(new Vector3(0, 1, 0)) /*?*/).toMatchSnapshot(); }); + + +test('should behave...', () => +{ + + let v = new Vector3(1, 0, 0); + rotateLine(v, 0.5); + + expect(v).toMatchSnapshot(); + + rotateLine(v, -0.5); + expect(v).toMatchSnapshot(); +}); diff --git a/__test__/Geometry/__snapshots__/GeUtils.test.ts.snap b/__test__/Geometry/__snapshots__/GeUtils.test.ts.snap index d3e0f02e1..72ebb847b 100644 --- a/__test__/Geometry/__snapshots__/GeUtils.test.ts.snap +++ b/__test__/Geometry/__snapshots__/GeUtils.test.ts.snap @@ -39,3 +39,19 @@ Vector3 { "z": 1, } `; + +exports[`should behave... 1`] = ` +Vector3 { + "x": 0.8775825618903728, + "y": 0.479425538604203, + "z": 0, +} +`; + +exports[`should behave... 2`] = ` +Vector3 { + "x": 1, + "y": 0, + "z": 0, +} +`; diff --git a/src/Common/CurveUtils.ts b/src/Common/CurveUtils.ts index eacba360f..cd1fbc311 100644 --- a/src/Common/CurveUtils.ts +++ b/src/Common/CurveUtils.ts @@ -3,6 +3,7 @@ import * as THREE from 'three'; import { CreateBoardUtil } from '../ApplicationServices/mesh/createBoard'; import { angleTo, equaln, equal } from '../Geometry/GeUtils'; +import { Matrix2 } from '../Geometry/Matrix2'; export enum ExtendDir { @@ -10,11 +11,10 @@ export enum ExtendDir Later = 1, Both = 2 } -// 旋转矢量 +//旋转矢量 export function rotateLine(l: Vector3, ang: number) { - let mat = new THREE.Matrix4().makeRotationZ(ang); - l.applyMatrix4(mat); + new Matrix2().setRotate(ang).applyVector(l); return l; } //2点加凸度获取圆弧面积 diff --git a/src/Geometry/Matrix2.ts b/src/Geometry/Matrix2.ts index 7f17c5507..29bae669e 100644 --- a/src/Geometry/Matrix2.ts +++ b/src/Geometry/Matrix2.ts @@ -1,8 +1,8 @@ -import { Vector2 } from "three"; +import { Vector2, Vector3 } from "three"; export class Matrix2 { - el = [1, 0, 0, 1]; + private el = [1, 0, 0, 1]; set(n11: number, n12: number, n21: number, n22: number) { let te = this.el; @@ -11,12 +11,14 @@ export class Matrix2 te[2] = n12; te[3] = n22; return this; } - applyVec2(vec: Vector2) + applyVector(vec: Vector2 | Vector3) { let x = vec.x, y = vec.y; let e = this.el; vec.x = e[0] * x + e[2] * y; vec.y = e[1] * x + e[3] * y; + + return this; } setRotate(theta) @@ -24,7 +26,8 @@ export class Matrix2 let el = this.el; let c = Math.cos(theta), s = Math.sin(theta); - el[0] = c; el[1] = -s; - el[2] = s; el[3] = c; + this.set(c, -s, + s, c); + return this; } } diff --git a/src/Geometry/RotateUV.ts b/src/Geometry/RotateUV.ts index 854317240..9a28d279a 100644 --- a/src/Geometry/RotateUV.ts +++ b/src/Geometry/RotateUV.ts @@ -16,10 +16,10 @@ export function RotateUVs(geo: THREE.Geometry) { for (let v of uv) { - roMat.applyVec2(v); + roMat.applyVector(v); v.add(addV); } } } geo.uvsNeedUpdate = true; -} \ No newline at end of file +}