AABB IsotropicParticleMesher::_getSliceAABB(int startidx, int endidx) {
	int width, height, depth;
	double dx;
	_getSubdividedGridDimensions(&width, &height, &depth, &dx);

	bool isStartSlice = startidx == 0;
	bool isEndSlice = endidx == width - 1;
	bool isMiddleSlice = !isStartSlice && !isEndSlice;

	double gridWidth = (endidx - startidx + 1)*dx;
	double gridHeight = height*dx;
	double gridDepth = depth*dx;
	if (isStartSlice || isEndSlice) {
		gridWidth += dx;
	} else if (isMiddleSlice) {
		gridWidth += 2.0*dx;
	}

	vmath::vec3 offset = _getSliceGridPositionOffset(startidx, endidx);

	AABB bbox(offset, gridWidth, gridHeight, gridDepth);
	bbox.expand(2.0*_particleRadius);

	return bbox;
}
TriangleMesh IsotropicParticleMesher::_polygonizeSlices(FragmentedVector<MarkerParticle> &particles, 
	                                                    FluidMaterialGrid &materialGrid) {
	int width, height, depth;
	double dx;
	_getSubdividedGridDimensions(&width, &height, &depth, &dx);

	int sliceWidth = ceil((double)width / (double)_numPolygonizationSlices);
	int numSlices = ceil((double)width / (double)sliceWidth);

	if (numSlices == 1) {
		return _polygonizeAll(particles, materialGrid);
	}

	TriangleMesh mesh;
	for (int i = 0; i < numSlices; i++) {
		int startidx = i*sliceWidth;
		int endidx = startidx + sliceWidth - 1;
		endidx = endidx < width ? endidx : width - 1;

		TriangleMesh sliceMesh = _polygonizeSlice(startidx, endidx, particles, materialGrid);

		vmath::vec3 offset = _getSliceGridPositionOffset(startidx, endidx);
		sliceMesh.translate(offset);
		mesh.join(sliceMesh);
	}

	return mesh;
}
void IsotropicParticleMesher::_computeSliceScalarField(int startidx, int endidx, 
	                                                   FragmentedVector<MarkerParticle> &markerParticles,
	                                                   FluidMaterialGrid &materialGrid,
	                                                   ImplicitSurfaceScalarField &field) {
	
	FragmentedVector<vmath::vec3> sliceParticles;
	_getSliceParticles(startidx, endidx, markerParticles, sliceParticles);

	int width, height, depth;
	field.getGridDimensions(&width, &height, &depth);

	FluidMaterialGrid sliceMaterialGrid(width - 1, height - 1, depth - 1);
	_getSliceMaterialGrid(startidx, endidx, materialGrid, sliceMaterialGrid);

	field.setMaterialGrid(sliceMaterialGrid);

	vmath::vec3 fieldOffset = _getSliceGridPositionOffset(startidx, endidx);
	field.setOffset(fieldOffset);
	field.setPointRadius(_particleRadius);

	for (unsigned int i = 0; i < sliceParticles.size(); i++) {
        field.addPoint(sliceParticles[i]);
    }

    _updateScalarFieldSeam(startidx, endidx, field);
}