Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Vec2 to Vec3 and vice versa #13994

Closed
minggo opened this issue Jan 3, 2023 · 12 comments
Closed

Convert Vec2 to Vec3 and vice versa #13994

minggo opened this issue Jan 3, 2023 · 12 comments
Assignees
Milestone

Comments

@minggo
Copy link
Contributor

minggo commented Jan 3, 2023

Some API returns Vec2, and some API accepts Vec3. It is not convenient. So need a way to convert types between Vec2 and Vec3. There are two ways to achieve it:

  • add constructor in Vec2 and Vec3, for example
class Vec2 {
    public constructor (v: Vec3);
}

class Vec3 {
    public constructor (v: Vec2);
}
  • add util functions to convert them
public vec2ToVec3 (v: Vec2) : Vec3 {
    ...
}

public vec3ToVec2 (v: Vec3) : Vec2 {
    ...
}

I think add constructors is better.

Can refer to this forum topic for detail information.

@AnkoGo123
Copy link

In any case, these transformations should not be perceived by the user, but should be placed within your system for transformations

@minggo
Copy link
Contributor Author

minggo commented Jan 20, 2023

@AnkoGo123 these parameter are passed in by developers, how to not be perceived by the user? Could you please describe it in more detail? Thanks.

@stanleyljl
Copy link
Contributor

How about add a toVec3 in vec2 & toVec2 in vec3? @minggo

@minggo
Copy link
Contributor Author

minggo commented Feb 9, 2023

How about add a toVec3 in vec2 & toVec2 in vec3? @minggo

It seems good to me. Could you please add function prototype?

@jareguo
Copy link
Contributor

jareguo commented Feb 11, 2023

@stanleyljl stanleyljl mentioned this issue Feb 27, 2023
6 tasks
@1226085293
Copy link

1226085293 commented Jul 17, 2023

在 vec2 中添加 toVec3 和在 vec3 中添加 toVec2 怎么样?@minggo

不明白为什么你觉得 toVecX 比构造更好点,那么比如 cc.Rect 这种数据结构呢?如果用 toVec2/toVec3,依旧只能 cc.v2(rect.x, rect.y), 用其他包含 x,y 属性的数据初始化也一样麻烦,但是只要 Vec2/Vec3 构造支持 { x?: number, y?: number, z?: number },能支持现在以及之后所有包含 xyz 参数的数据直接初始化 Vec2/Vec3

@minggo
Copy link
Contributor Author

minggo commented Jul 17, 2023

@1226085293 we do not want to use duck type as possible. Duck type is bad for performance.

@1226085293
Copy link

@1226085293 we do not want to use duck type as possible. Duck type is bad for performance.

那么直接在构造内针对类型单独处理如何?示例如下

class Vec2 {
	x: number;
	y: number;
}
class Vec3 {
	x: number;
	y: number;
	z: number;
	constructor(value_: { x?: number; y?: number; z?: number }) {
		if (value_ instanceof Vec2) {
			this.x = value_.x;
			this.y = value_.y;
			this.z = 0;
		} else {
			this.x = value_.x ?? 0;
			this.y = value_.y ?? 0;
			this.z = value_.z ?? 0;
		}
	}
}

@minggo
Copy link
Contributor Author

minggo commented Jul 18, 2023

I think it is duck type too. Am i right?
And please use english in future.

@1226085293
Copy link

I think it is duck type too. Am i right? And please use english in future.

如果把初始化参数改成对象类型甚至还提高了性能,但是按照你们对兼容性的做法估计也不会改,按照现有的构造参数类型去改就会是你说的性能浪费

image

class Vec2 {
	x = 0;
	y = 0;
}

class Vec3 {
	x = 0;
	y = 0;
	z = 0;
	constructor(v: Vec3);

	constructor(x?: number, y?: number, z?: number);

	constructor(x?: number | Vec3, y?: number, z?: number) {
		if (typeof x === "object") {
			this.x = x.x;
			this.y = x.y;
			this.z = x.z;
		} else {
			this.x = x || 0;
			this.y = y || 0;
			this.z = z || 0;
		}
	}
}

class Vec3_2 {
	x = 0;
	y = 0;
	z = 0;
	constructor(value_: { x?: number; y?: number; z?: number }) {
		this.x = value_.x ?? 0;
		this.y = value_.y ?? 0;
		this.z = value_.z ?? 0;
	}
}

let for_n = 100000000;
{
	let time_start_n = Date.now();

	for (let k_n = 0; k_n < for_n; ++k_n) {
		new Vec3(k_n, k_n + 1);
	}

	console.log("Vec3 使用 number 初始化", Date.now() - time_start_n);
}

{
	let time_start_n = Date.now();
	let temp_v3 = new Vec3();
	for (let k_n = 0; k_n < for_n; ++k_n) {
		temp_v3.x = k_n;
		temp_v3.y = k_n + 1;
		temp_v3.z = k_n + 2;
		new Vec3(temp_v3);
	}

	console.log("Vec3 使用 Vec3 初始化", Date.now() - time_start_n);
}

{
	let time_start_n = Date.now();
	let temp_v2 = new Vec2();
	for (let k_n = 0; k_n < for_n; ++k_n) {
		temp_v2.x = k_n;
		temp_v2.y = k_n + 1;
		new Vec3(temp_v2.x, temp_v2.y);
	}

	console.log("Vec3 使用 Vec2 初始化", Date.now() - time_start_n);
}

{
	let time_start_n = Date.now();
	for (let k_n = 0; k_n < for_n; ++k_n) {
		new Vec3_2({
			x: k_n,
			y: k_n + 1,
		});
	}

	console.log("Vec3_2 使用 number 初始化", Date.now() - time_start_n);
}

{
	let time_start_n = Date.now();
	let temp_v3 = new Vec3_2({});
	for (let k_n = 0; k_n < for_n; ++k_n) {
		temp_v3.x = k_n;
		temp_v3.y = k_n + 1;
		temp_v3.z = k_n + 2;
		new Vec3_2(temp_v3);
	}

	console.log("Vec3_2 使用 Vec3_2 初始化", Date.now() - time_start_n);
}

{
	let time_start_n = Date.now();
	let temp_v2 = new Vec2();
	for (let k_n = 0; k_n < for_n; ++k_n) {
		temp_v2.x = k_n;
		temp_v2.y = k_n + 1;
		new Vec3_2(temp_v2);
	}

	console.log("Vec3_2 使用 Vec2 初始化", Date.now() - time_start_n);
}

@minggo minggo assigned minggo and unassigned stanleyljl Oct 24, 2023
@minggo
Copy link
Contributor Author

minggo commented Apr 16, 2024

So i think it is still better to implement it as static functions.

@minggo minggo added this to the 3.8.4 milestone Apr 16, 2024
@minggo
Copy link
Contributor Author

minggo commented Apr 17, 2024

Sorry, i mean to implement it as member functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants