{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f9607bd1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of remeshing iterations: 1\n",
      "(25568, 3, 3)\n"
     ]
    }
   ],
   "source": [
    "from deltamic import Gaussian_psf, compute_spatial_frequency_grid,normalize_tensor,render_image_from_ftmesh,Fourier3dMesh,compute_box_size\n",
    "from deltamic.mask_creation import create_instance_segmentation_mask,create_semantic_segmentation_mask\n",
    "import trimesh\n",
    "import numpy as np\n",
    "import torch\n",
    "import skimage.io as io \n",
    "from skimage import img_as_ubyte\n",
    "from time import time\n",
    "from dw3d import open_mesh_multitracker, write_mesh_bin, DCEL_Data\n",
    "\n",
    "filename = \"../data/mesh_multimaterial.rec\"\n",
    "Verts,Faces_mm,_ = open_mesh_multitracker(filename)\n",
    "box_shape = np.array([30,30,30])\n",
    "box_size = compute_box_size(Verts,offset=0.1)\n",
    "labels = create_instance_segmentation_mask(Verts,Faces_mm,box_shape,box_size)\n",
    "contours = create_semantic_segmentation_mask(Verts,Faces_mm[:,:3],box_shape,box_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f07cfcf0",
   "metadata": {},
   "outputs": [],
   "source": [
    "device = 'cpu'\n",
    "\n",
    "Verts = torch.tensor(Verts,dtyadd_image=torch.float).to(device)\n",
    "Faces_mm = torch.tensor(Faces_mm,dtype = torch.long).to(device)\n",
    "Faces = Faces_mm[:,:3].contiguous()\n",
    "print(\"Number of verts,faces\",len(Verts),len(Faces_mm))\n",
    "Faces_coeff = torch.zeros(len(Faces),dtype = torch.float, device = device)\n",
    "for i,f in enumerate(Faces_mm.cpu().numpy()): \n",
    "    if f[3]==0 or f[4]==0:  \n",
    "        Faces_coeff[i]=1\n",
    "    else : \n",
    "        Faces_coeff[i]=2\n",
    "        \n",
    "print(\"Vertices shape: \",Verts.shape,\"Faces shape: \", Faces.shape)\n",
    "print(\"Min/Max x/y/z position of vertices: \",Verts.min(axis=0),Verts.max(axis=0))\n",
    "print(\"box_size: \",box_size, \"box_shape\",box_shape)\n",
    "\n",
    "narrowband_thresh = torch.tensor(0,dtype = torch.float, device = device)\n",
    "\n",
    "model = Gaussian_psf(box_shape,box_size[:,1],sigma = 1e3,device = device)\n",
    "PSF = model.forward()\n",
    "OTF = torch.abs(torch.fft.fftshift(torch.fft.fftn(PSF)))\n",
    "\n",
    "meshFT = Fourier3dMesh(box_size,list(box_shape),device=device, dtype = torch.float32)\n",
    "ftmesh = meshFT(Verts,Faces, Faces_coeff)\n",
    "image_ft=normalize_tensor(render_image_from_ftmesh(ftmesh, OTF, list(box_shape)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "906a726b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import napari\n",
    "v = napari.view_image(labels)\n",
    "v.add_image(contours)\n",
    "v.add_image(image_ft.detach().cpu().numpy())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
