# \[javascript\] Deform polydata using known segments for each cell

**URL:** https://discourse.vtk.org/t/javascript-deform-polydata-using-known-segments-for-each-cell/11730
**Category:** Support
**Tags:** vtkjs
**Created:** [June 15, 2023, 12:05pm UTC](https://discourse.vtk.org/t/javascript-deform-polydata-using-known-segments-for-each-cell/11730 "2023-06-15T12:05:30Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Alex\_Vergara](https://discourse.vtk.org/user_avatar/discourse.vtk.org/alex_vergara/32/7082_2.png) [@Alex\_Vergara](https://discourse.vtk.org/u/Alex_Vergara)
#### Post date: [June 15, 2023, 12:05pm UTC](https://discourse.vtk.org/t/javascript-deform-polydata-using-known-segments-for-each-cell/11730/1 "2023-06-15T12:05:30Z")

</div>

Basically, I need to replicate this [BMI Visualizer - BMIWebgl | Perceiving Systems](https://www.bmivisualizer.com/). They define a set of points and a function for each of them depending on height and weight.

I have polydata models and I need to warp them based on patient weight and height. Any hints? Does anyone have some working models?

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [June 16, 2023, 7:06pm UTC](https://discourse.vtk.org/t/javascript-deform-polydata-using-known-segments-for-each-cell/11730/2 "2023-06-16T19:06:27Z")

</div>

Hello,

Based on that visualizer, here’s my two cents: I believe they defined some key point clouds for some parameters (e.g. 1.70m 70kg male, 1.80m 70kg male, 1.70m 80kg male…) and use some point cloud morphing algorithm to compute and render intermediate body models for non-key parameters on the fly. I don’t believe simple linear scaling would result in realistic body models.

take care,

Paulo

---

<div class="post-metadata">

### Author: ![Alex\_Vergara](https://discourse.vtk.org/user_avatar/discourse.vtk.org/alex_vergara/32/7082_2.png) [@Alex\_Vergara](https://discourse.vtk.org/u/Alex_Vergara)
#### Post date: [June 30, 2023, 2:41pm UTC](https://discourse.vtk.org/t/javascript-deform-polydata-using-known-segments-for-each-cell/11730/3 "2023-06-30T14:41:34Z")

</div>

Their models are free to use

[Male](https://www.bmivisualizer.com/models/male_measurement_data.json) and [Female](https://www.bmivisualizer.com/models/female_measurement_data.json)

using this functions you can achieve what I want (And probably what they did)

```js
    function getCoeff(value, min, max){
		ymin = -100;
		ymax = 100;
		if (value<min) {
			return ymin;
		};
		if (value>max){
			return ymax;
		};
		return ymin + (ymax-ymin)*(value-min)/(max-min);
	}

	function readJSONPolyData(modelJSON, height, weight){
		const coeff = [
			getCoeff(weight, modelJSON["measurements_min"]["weight"], modelJSON["measurements_max"]["weight"]),
			getCoeff(height*10, modelJSON["measurements_min"]["stature"], modelJSON["measurements_max"]["stature"])
		];
		let points = vtk.Common.Core.vtkPoints.newInstance();
		for (let i=0; i<modelJSON["vertex_means"].length/3; i+=1){
			let p=[0,0,0];
			for (let j=0; j<3; j+=1){
				p[j] = modelJSON["vertex_means"][i*3+j] + coeff[0]*modelJSON["vertex_offsets"]["weight"][i*3+j] + coeff[1]*modelJSON["vertex_offsets"]["stature"][i*3+j];
			};	
			points.insertNextPoint(p[0], p[1], p[2]);
		};

		cells = vtk.Common.Core.vtkCellArray.newInstance();
		for (let i=0; i<modelJSON["faces"].length; i+=1){
			cells.insertNextCell(modelJSON["faces"][i]);
		};

		trianglePolyData = vtk.Common.DataModel.vtkPolyData.newInstance();
		trianglePolyData.setPoints(points);
		trianglePolyData.setPolys(cells);

		return trianglePolyData;
	}

```
