Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

""" 

Indexed Face Sets 

  

Graphics3D object that consists of a list of polygons, also used for 

triangulations of other objects. 

  

Usually these objects are not created directly by users. 

  

AUTHORS: 

  

- Robert Bradshaw (2007-08-26): initial version 

- Robert Bradshaw (2007-08-28): significant optimizations 

  

.. TODO:: 

  

Smooth triangles using vertex normals 

  

""" 

#***************************************************************************** 

# Copyright (C) 2007 Robert Bradshaw <robertwb@math.washington.edu> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import print_function, absolute_import 

  

from libc.math cimport isfinite, INFINITY 

from libc.string cimport memset, memcpy 

from cysignals.memory cimport check_calloc, check_allocarray, check_reallocarray, sig_free 

from cysignals.signals cimport sig_check, sig_on, sig_off 

  

cdef extern from *: 

int sprintf_3d "sprintf" (char*, char*, double, double, double) 

int sprintf_3i "sprintf" (char*, char*, int, int, int) 

int sprintf_4i "sprintf" (char*, char*, int, int, int, int) 

int sprintf_5i "sprintf" (char*, char*, int, int, int, int, int) 

int sprintf_6i "sprintf" (char*, char*, int, int, int, int, int, int) 

int sprintf_7i "sprintf" (char*, char*, int, int, int, int, int, int, int) 

int sprintf_9d "sprintf" (char*, char*, double, double, double, double, double, double, double, double, double) 

  

from cpython.list cimport * 

from cpython.bytes cimport * 

  

include "point_c.pxi" 

  

  

from math import sin, cos, sqrt 

from random import randint 

  

from sage.cpython.string cimport bytes_to_str 

  

from sage.rings.real_double import RDF 

  

from sage.matrix.constructor import matrix 

from sage.modules.free_module_element import vector 

  

from sage.plot.colors import Color, float_to_integer 

from sage.plot.plot3d.base import Graphics3dGroup 

  

from .transform cimport Transformation 

  

  

# -------------------------------------------------------------------- 

# Fast routines for generating string representations of the polygons. 

# -------------------------------------------------------------------- 

  

cdef inline format_tachyon_texture(color_c rgb): 

cdef char rs[200] 

cdef Py_ssize_t cr = sprintf_3d(rs, 

"TEXTURE\n AMBIENT 0.3 DIFFUSE 0.7 SPECULAR 0 OPACITY 1.0\n COLOR %g %g %g \n TEXFUNC 0", 

rgb.r, rgb.g, rgb.b) 

return bytes_to_str(PyBytes_FromStringAndSize(rs, cr)) 

  

  

cdef inline format_tachyon_triangle(point_c P, point_c Q, point_c R): 

cdef char ss[250] 

# PyBytes_FromFormat doesn't do floats? 

cdef Py_ssize_t r = sprintf_9d(ss, 

"TRI V0 %g %g %g V1 %g %g %g V2 %g %g %g", 

P.x, P.y, P.z, 

Q.x, Q.y, Q.z, 

R.x, R.y, R.z ) 

return bytes_to_str(PyBytes_FromStringAndSize(ss, r)) 

  

  

cdef inline format_json_vertex(point_c P): 

cdef char ss[100] 

cdef Py_ssize_t r = sprintf_3d(ss, '{"x":%g,"y":%g,"z":%g}', P.x, P.y, P.z) 

return bytes_to_str(PyBytes_FromStringAndSize(ss, r)) 

  

cdef inline format_json_face(face_c face): 

s = "[{}]".format(",".join([str(face.vertices[i]) 

for i from 0 <= i < face.n])) 

return s 

  

cdef inline format_obj_vertex(point_c P): 

cdef char ss[100] 

# PyBytes_FromFormat doesn't do floats? 

cdef Py_ssize_t r = sprintf_3d(ss, "v %g %g %g", P.x, P.y, P.z) 

return bytes_to_str(PyBytes_FromStringAndSize(ss, r)) 

  

cdef inline format_obj_face(face_c face, int off): 

cdef char ss[100] 

cdef Py_ssize_t r, i 

if face.n == 3: 

r = sprintf_3i(ss, "f %d %d %d", face.vertices[0] + off, face.vertices[1] + off, face.vertices[2] + off) 

elif face.n == 4: 

r = sprintf_4i(ss, "f %d %d %d %d", face.vertices[0] + off, face.vertices[1] + off, face.vertices[2] + off, face.vertices[3] + off) 

else: 

return "f " + " ".join([str(face.vertices[i] + off) for i from 0 <= i < face.n]) 

# PyBytes_FromFormat is almost twice as slow 

return bytes_to_str(PyBytes_FromStringAndSize(ss, r)) 

  

cdef inline format_obj_face_back(face_c face, int off): 

cdef char ss[100] 

cdef Py_ssize_t r, i 

if face.n == 3: 

r = sprintf_3i(ss, "f %d %d %d", face.vertices[2] + off, face.vertices[1] + off, face.vertices[0] + off) 

elif face.n == 4: 

r = sprintf_4i(ss, "f %d %d %d %d", face.vertices[3] + off, face.vertices[2] + off, face.vertices[1] + off, face.vertices[0] + off) 

else: 

return "f " + " ".join([str(face.vertices[i] + off) for i from face.n > i >= 0]) 

return bytes_to_str(PyBytes_FromStringAndSize(ss, r)) 

  

cdef inline format_pmesh_vertex(point_c P): 

cdef char ss[100] 

# PyBytes_FromFormat doesn't do floats? 

cdef Py_ssize_t r = sprintf_3d(ss, "%g %g %g", P.x, P.y, P.z) 

return bytes_to_str(PyBytes_FromStringAndSize(ss, r)) 

  

cdef inline format_pmesh_face(face_c face, int has_color): 

cdef char ss[100] 

cdef Py_ssize_t r, i 

cdef int color 

# if the face has an individual color, has_color is -1 

# otherwise it is 1 

if has_color == -1: 

color = float_to_integer(face.color.r, 

face.color.g, 

face.color.b) 

# it seems that Jmol does not like the 0 color at all 

if color == 0: 

color = 1 

  

if face.n == 3: 

if has_color == 1: 

r = sprintf_5i(ss, "%d\n%d\n%d\n%d\n%d", has_color * 4, 

face.vertices[0], 

face.vertices[1], 

face.vertices[2], 

face.vertices[0]) 

else: 

r = sprintf_6i(ss, "%d\n%d\n%d\n%d\n%d\n%d", has_color * 4, 

face.vertices[0], 

face.vertices[1], 

face.vertices[2], 

face.vertices[0], color) 

elif face.n == 4: 

if has_color == 1: 

r = sprintf_6i(ss, "%d\n%d\n%d\n%d\n%d\n%d", has_color * 5, 

face.vertices[0], 

face.vertices[1], 

face.vertices[2], 

face.vertices[3], 

face.vertices[0]) 

else: 

r = sprintf_7i(ss, "%d\n%d\n%d\n%d\n%d\n%d\n%d", has_color * 5, 

face.vertices[0], 

face.vertices[1], 

face.vertices[2], 

face.vertices[3], 

face.vertices[0], color) 

else: 

# Naive triangulation 

all = [] 

if has_color == 1: 

for i from 1 <= i < face.n - 1: 

r = sprintf_5i(ss, "%d\n%d\n%d\n%d\n%d", has_color * 4, 

face.vertices[0], 

face.vertices[i], 

face.vertices[i + 1], 

face.vertices[0]) 

PyList_Append(all, PyBytes_FromStringAndSize(ss, r)) 

else: 

for i from 1 <= i < face.n - 1: 

r = sprintf_6i(ss, "%d\n%d\n%d\n%d\n%d\n%d", has_color * 4, 

face.vertices[0], 

face.vertices[i], 

face.vertices[i + 1], 

face.vertices[0], color) 

PyList_Append(all, PyBytes_FromStringAndSize(ss, r)) 

return bytes_to_str(b"\n".join(all)) 

# PyBytes_FromFormat is almost twice as slow 

return bytes_to_str(PyBytes_FromStringAndSize(ss, r)) 

  

  

cdef class IndexFaceSet(PrimitiveObject): 

""" 

Graphics3D object that consists of a list of polygons, also used for 

triangulations of other objects. 

  

Polygons (mostly triangles and quadrilaterals) are stored in the 

c struct ``face_c`` (see transform.pyx). Rather than storing 

the points directly for each polygon, each face consists a list 

of pointers into a common list of points which are basically triples 

of doubles in a ``point_c``. 

  

Moreover, each face has an attribute ``color`` which is used to 

store color information when faces are colored. The red/green/blue 

components are then available as floats between 0 and 1 using 

``color.r,color.g,color.b``. 

  

Usually these objects are not created directly by users. 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.index_face_set import IndexFaceSet 

sage: S = IndexFaceSet([[(1,0,0),(0,1,0),(0,0,1)],[(1,0,0),(0,1,0),(0,0,0)]]) 

sage: S.face_list() 

[[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)], [(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 0.0)]] 

sage: S.vertex_list() 

[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0), (0.0, 0.0, 0.0)] 

  

sage: def make_face(n): return [(0,0,n),(0,1,n),(1,1,n),(1,0,n)] 

sage: S = IndexFaceSet([make_face(n) for n in range(10)]) 

sage: S.show() 

  

sage: point_list = [(1,0,0),(0,1,0)] + [(0,0,n) for n in range(10)] 

sage: face_list = [[0,1,n] for n in range(2,10)] 

sage: S = IndexFaceSet(face_list, point_list, color='red') 

sage: S.face_list() 

[[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 0.0)], 

[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0)], 

[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 2.0)], 

[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 3.0)], 

[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 4.0)], 

[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 5.0)], 

[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 6.0)], 

[(1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 7.0)]] 

sage: S.show() 

  

A simple example of colored IndexFaceSet (:trac:`12212`):: 

  

sage: from sage.plot.plot3d.index_face_set import IndexFaceSet 

sage: from sage.plot.plot3d.texture import Texture 

sage: point_list = [(2,0,0),(0,2,0),(0,0,2),(0,1,1),(1,0,1),(1,1,0)] 

sage: face_list = [[0,4,5],[3,4,5],[2,3,4],[1,3,5]] 

sage: col = rainbow(10, 'rgbtuple') 

sage: t_list = [Texture(col[i]) for i in range(10)] 

sage: S = IndexFaceSet(face_list, point_list, texture_list=t_list) 

sage: S.show(viewer='tachyon') 

  

""" 

def __cinit__(self): 

self.vs = <point_c *>NULL 

self.face_indices = <int *>NULL 

self._faces = <face_c *>NULL 

def __init__(self, faces, point_list=None, 

enclosed=False, texture_list=None, **kwds): 

PrimitiveObject.__init__(self, **kwds) 

  

self.global_texture = (texture_list is None) 

  

self.enclosed = enclosed 

  

if point_list is None: 

face_list = faces 

faces = [] 

point_list = [] 

point_index = {} 

for face in face_list: 

iface = [] 

for p in face: 

try: 

ix = point_index[p] 

except KeyError: 

ix = len(point_list) 

point_index[p] = ix 

point_list.append(p) 

iface.append(ix) 

faces.append(iface) 

  

cdef Py_ssize_t i 

cdef Py_ssize_t index_len = 0 

for i from 0 <= i < len(faces): 

index_len += len(faces[i]) 

  

self.vcount = len(point_list) 

self.fcount = len(faces) 

self.icount = index_len 

  

self.realloc(self.vcount, self.fcount, index_len) 

  

for i from 0 <= i < self.vcount: 

self.vs[i].x, self.vs[i].y, self.vs[i].z = point_list[i] 

  

cdef int cur_pt = 0 

for i from 0 <= i < self.fcount: 

self._faces[i].n = len(faces[i]) 

self._faces[i].vertices = &self.face_indices[cur_pt] 

if self.global_texture: 

self._faces[i].color.r, self._faces[i].color.g, self._faces[i].color.b = self.texture.color 

else: 

self._faces[i].color.r, self._faces[i].color.g, self._faces[i].color.b = texture_list[i].color 

for ix in faces[i]: 

self.face_indices[cur_pt] = ix 

cur_pt += 1 

  

cdef realloc(self, vcount, fcount, icount): 

r""" 

Allocates memory for vertices, faces, and face indices. Can 

only be called from Cython, so the doctests must be indirect. 

  

EXAMPLES:: 

  

sage: var('x,y,z') 

(x, y, z) 

sage: G = implicit_plot3d(x^2+y^2+z^2 - 1, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=6) 

sage: G.triangulate() # indirect doctest 

sage: len(G.face_list()) 

44 

sage: len(G.vertex_list()) 

132 

sage: G = implicit_plot3d(x^2+y^2+z^2 - 100, (x, -2, 2), (y, -2, 2), (z, -2, 2), plot_points=6) 

sage: G.triangulate() # indirect doctest 

sage: len(G.face_list()) 

0 

sage: len(G.vertex_list()) 

0 

""" 

self.vs = <point_c *>check_reallocarray(self.vs, vcount, sizeof(point_c)) 

self._faces = <face_c *>check_reallocarray(self._faces, fcount, sizeof(face_c)) 

self.face_indices = <int *>check_reallocarray(self.face_indices, icount, sizeof(int)) 

  

def _clean_point_list(self): 

""" 

Clean up the vertices and faces as follows: 

  

- Remove all vertices with a coordinate which is NaN or 

infinity. 

  

- If a removed vertex occurs in a face, remove it from that 

face, but keep other vertices in that face. 

  

- Remove faces with less than 3 vertices. 

  

- Remove unused vertices. 

  

- Free unused memory for vertices and faces (not indices). 

""" 

cdef Py_ssize_t i, j, v 

  

# point_map is an array old vertex index -> new vertex index. 

# The special value -1 means that the vertex is not mapped yet. 

# The special value -2 means that the vertex must be deleted 

# because a coordinate is NaN or infinity. 

# When we are done, all vertices with negative indices are not 

# used and will be removed. 

cdef int* point_map = <int*>check_allocarray(self.vcount, sizeof(int)) 

  

cdef Py_ssize_t nv = 0 # number of new vertices 

for i in range(self.vcount): 

point_map[i] = -1 

  

# Process all faces 

cdef Py_ssize_t nf = 0 # number of new faces 

cdef Py_ssize_t fv # number of new vertices on face 

for i in range(self.fcount): 

face = &self._faces[i] 

  

# Process vertices in face 

fv = 0 

for j in range(face.n): 

v = face.vertices[j] 

if point_map[v] == -1: 

pt = &self.vs[v] 

if isfinite(pt.x) and isfinite(pt.y) and isfinite(pt.z): 

point_map[v] = nv 

nv += 1 

else: 

point_map[v] = -2 

if point_map[v] == -2: 

continue 

  

face.vertices[fv] = point_map[face.vertices[j]] 

fv += 1 

  

# Skip faces with less than 3 vertices 

if fv < 3: 

continue 

  

# Store in newface 

newface = &self._faces[nf] 

newface.n = fv 

if newface is not face: 

newface.vertices = face.vertices 

newface.color = face.color 

nf += 1 

  

# Realloc face array 

if nf < self.fcount: 

self._faces = <face_c*>check_reallocarray(self._faces, nf, sizeof(face_c)) 

self.fcount = nf 

  

# Realloc and map vertex array 

# We cannot copy in-place since we permuted the vertices 

new_vs = <point_c*>check_allocarray(nv, sizeof(point_c)) 

for i in range(self.vcount): 

j = point_map[i] 

if j >= 0: 

new_vs[j] = self.vs[i] 

  

sig_free(point_map) 

sig_free(self.vs) 

self.vs = new_vs 

self.vcount = nv 

  

def _separate_creases(self, threshold): 

""" 

Some rendering engines Gouraud shading, which is great for smooth 

surfaces but looks bad if one actually has a polyhedron. 

  

INPUT: 

  

``threshold`` -- the minimum cosine of the angle between adjacent 

faces a higher threshold separates more, all faces if >= 1, no 

faces if <= -1 

""" 

cdef Py_ssize_t i, j, k 

cdef face_c *face 

cdef int v, count, total = 0 

cdef int* point_counts = <int *>check_calloc(self.vcount * 2 + 1, sizeof(int)) 

# For each vertex, get number of faces 

cdef int* running_point_counts = &point_counts[self.vcount] 

for i from 0 <= i < self.fcount: 

face = &self._faces[i] 

total += face.n 

for j from 0 <= j < face.n: 

point_counts[face.vertices[j]] += 1 

# Running used as index into face list 

cdef int running = 0 

cdef int max = 0 

for i from 0 <= i < self.vcount: 

running_point_counts[i] = running 

running += point_counts[i] 

if point_counts[i] > max: 

max = point_counts[i] 

running_point_counts[self.vcount] = running 

# Create an array, indexed by running_point_counts[v], to the list of faces containing that vertex. 

cdef face_c** point_faces 

try: 

point_faces = <face_c **>check_allocarray(total, sizeof(face_c*)) 

except MemoryError: 

sig_free(point_counts) 

raise 

sig_on() 

memset(point_counts, 0, sizeof(int) * self.vcount) 

for i from 0 <= i < self.fcount: 

face = &self._faces[i] 

for j from 0 <= j < face.n: 

v = face.vertices[j] 

point_faces[running_point_counts[v]+point_counts[v]] = face 

point_counts[v] += 1 

# Now, for each vertex, see if all faces are close enough, 

# or if it is a crease. 

cdef face_c** faces 

cdef int start = 0 

cdef bint any 

# We compare against face 0, and if it's not flat enough we push it to the end. 

# Then we come around again to compare everything that was put at the end, possibly 

# pushing stuff to the end again (until no further changes are needed). 

while start < self.vcount: 

ix = self.vcount 

# Find creases 

for i from 0 <= i < self.vcount - start: 

faces = &point_faces[running_point_counts[i]] 

any = 0 

for j from point_counts[i] > j >= 1: 

if cos_face_angle(faces[0][0], faces[j][0], self.vs) < threshold: 

any = 1 

face = faces[j] 

point_counts[i] -= 1 

if j != point_counts[i]: 

faces[j] = faces[point_counts[i]] # swap 

faces[point_counts[i]] = face 

if any: 

ix += 1 

# Reallocate room for vertices at end 

if ix > self.vcount: 

try: 

self.vs = <point_c *>check_reallocarray(self.vs, ix, sizeof(point_c)) 

except MemoryError: 

sig_free(point_counts) 

sig_free(point_faces) 

self.vcount = self.fcount = self.icount = 0 # so we don't get segfaults on bad points 

sig_off() 

raise 

ix = self.vcount 

running = 0 

for i from 0 <= i < self.vcount - start: 

if point_counts[i] != running_point_counts[i+1] - running_point_counts[i]: 

# We have a new vertex 

self.vs[ix] = self.vs[i+start] 

# Update the point_counts and point_faces arrays for the next time around. 

count = running_point_counts[i+1] - running_point_counts[i] - point_counts[i] 

faces = &point_faces[running] 

for j from 0 <= j < count: 

faces[j] = point_faces[running_point_counts[i] + point_counts[i] + j] 

face = faces[j] 

for k from 0 <= k < face.n: 

if face.vertices[k] == i + start: 

face.vertices[k] = ix 

point_counts[ix-self.vcount] = count 

running_point_counts[ix-self.vcount] = running 

running += count 

ix += 1 

running_point_counts[ix-self.vcount] = running 

start = self.vcount 

self.vcount = ix 

  

sig_free(point_counts) 

sig_free(point_faces) 

sig_off() 

  

def _mem_stats(self): 

return self.vcount, self.fcount, self.icount 

  

def __dealloc__(self): 

sig_free(self.vs) 

sig_free(self._faces) 

sig_free(self.face_indices) 

  

def is_enclosed(self): 

""" 

Whether or not it is necessary to render the back sides of the polygons. 

  

One is assuming, of course, that they have the correct orientation. 

  

This is may be passed in on construction. It is also 

calculated in 

:class:`sage.plot.plot3d.parametric_surface.ParametricSurface` 

by verifying the opposite edges of the rendered domain either 

line up or are pinched together. 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.index_face_set import IndexFaceSet 

sage: IndexFaceSet([[(0,0,1),(0,1,0),(1,0,0)]]).is_enclosed() 

False 

""" 

return self.enclosed 

  

def index_faces(self): 

""" 

Return the list over all faces of the indices of the vertices. 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Box(1,2,3) 

sage: S.index_faces() 

[[0, 1, 2, 3], 

[0, 4, 5, 1], 

[0, 3, 6, 4], 

[5, 4, 6, 7], 

[6, 3, 2, 7], 

[2, 1, 5, 7]] 

""" 

cdef Py_ssize_t i, j 

return [[self._faces[i].vertices[j] 

for j from 0 <= j < self._faces[i].n] 

for i from 0 <= i < self.fcount] 

  

def has_local_colors(self): 

""" 

Return ``True`` if and only if every face has an individual color. 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.index_face_set import IndexFaceSet 

sage: from sage.plot.plot3d.texture import Texture 

sage: point_list = [(2,0,0),(0,2,0),(0,0,2),(0,1,1),(1,0,1),(1,1,0)] 

sage: face_list = [[0,4,5],[3,4,5],[2,3,4],[1,3,5]] 

sage: col = rainbow(10, 'rgbtuple') 

sage: t_list=[Texture(col[i]) for i in range(10)] 

sage: S = IndexFaceSet(face_list, point_list, texture_list=t_list) 

sage: S.has_local_colors() 

True 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Box(1,2,3) 

sage: S.has_local_colors() 

False 

""" 

return not(self.global_texture) 

  

def index_faces_with_colors(self): 

""" 

Return the list over all faces of (indices of the vertices, color). 

  

This only works if every face has its own color. 

  

.. SEEALSO:: 

  

:meth:`has_local_colors` 

  

EXAMPLES: 

  

A simple colored one:: 

  

sage: from sage.plot.plot3d.index_face_set import IndexFaceSet 

sage: from sage.plot.plot3d.texture import Texture 

sage: point_list = [(2,0,0),(0,2,0),(0,0,2),(0,1,1),(1,0,1),(1,1,0)] 

sage: face_list = [[0,4,5],[3,4,5],[2,3,4],[1,3,5]] 

sage: col = rainbow(10, 'rgbtuple') 

sage: t_list=[Texture(col[i]) for i in range(10)] 

sage: S = IndexFaceSet(face_list, point_list, texture_list=t_list) 

sage: S.index_faces_with_colors() 

[([0, 4, 5], '#ff0000'), 

([3, 4, 5], '#ff9900'), 

([2, 3, 4], '#cbff00'), 

([1, 3, 5], '#33ff00')] 

  

When the texture is global, an error is raised:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Box(1,2,3) 

sage: S.index_faces_with_colors() 

Traceback (most recent call last): 

... 

ValueError: the texture is global 

""" 

cdef Py_ssize_t i, j 

if self.global_texture: 

raise ValueError('the texture is global') 

return [([self._faces[i].vertices[j] 

for j from 0 <= j < self._faces[i].n], 

Color(self._faces[i].color.r, 

self._faces[i].color.g, 

self._faces[i].color.b).html_color()) 

for i from 0 <= i < self.fcount] 

  

def faces(self): 

""" 

An iterator over the faces. 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Box(1,2,3) 

sage: list(S.faces()) == S.face_list() 

True 

""" 

return FaceIter(self) 

  

def face_list(self): 

""" 

Return the list of faces. 

  

Every face is given as a tuple of vertices. 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Box(1,2,3) 

sage: S.face_list()[0] 

[(1.0, 2.0, 3.0), (-1.0, 2.0, 3.0), (-1.0, -2.0, 3.0), (1.0, -2.0, 3.0)] 

""" 

points = self.vertex_list() 

cdef Py_ssize_t i, j 

return [[points[self._faces[i].vertices[j]] 

for j from 0 <= j < self._faces[i].n] 

for i from 0 <= i < self.fcount] 

  

def edges(self): 

""" 

An iterator over the edges. 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Box(1,2,3) 

sage: list(S.edges())[0] 

((1.0, -2.0, 3.0), (1.0, 2.0, 3.0)) 

""" 

return EdgeIter(self) 

  

def edge_list(self): 

""" 

Return the list of edges. 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Box(1,2,3) 

sage: S.edge_list()[0] 

((1.0, -2.0, 3.0), (1.0, 2.0, 3.0)) 

""" 

return list(self.edges()) 

  

def vertices(self): 

""" 

An iterator over the vertices. 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Cone(1,1) 

sage: list(S.vertices()) == S.vertex_list() 

True 

""" 

return VertexIter(self) 

  

def vertex_list(self): 

""" 

Return the list of vertices. 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = polygon([(0,0,1), (1,1,1), (2,0,1)]) 

sage: S.vertex_list()[0] 

(0.0, 0.0, 1.0) 

""" 

cdef Py_ssize_t i 

return [(self.vs[i].x, self.vs[i].y, self.vs[i].z) for i from 0 <= i < self.vcount] 

  

def x3d_geometry(self): 

""" 

Return the x3d data. 

  

EXAMPLES: 

  

A basic test with a triangle:: 

  

sage: G = polygon([(0,0,1), (1,1,1), (2,0,1)]) 

sage: print(G.x3d_geometry()) 

<BLANKLINE> 

<IndexedFaceSet coordIndex='0,1,2,-1'> 

<Coordinate point='0.0 0.0 1.0,1.0 1.0 1.0,2.0 0.0 1.0'/> 

</IndexedFaceSet> 

<BLANKLINE> 

  

A simple colored one:: 

  

sage: from sage.plot.plot3d.index_face_set import IndexFaceSet 

sage: from sage.plot.plot3d.texture import Texture 

sage: point_list = [(2,0,0),(0,2,0),(0,0,2),(0,1,1),(1,0,1),(1,1,0)] 

sage: face_list = [[0,4,5],[3,4,5],[2,3,4],[1,3,5]] 

sage: col = rainbow(10, 'rgbtuple') 

sage: t_list=[Texture(col[i]) for i in range(10)] 

sage: S = IndexFaceSet(face_list, point_list, texture_list=t_list) 

sage: print(S.x3d_geometry()) 

<BLANKLINE> 

<IndexedFaceSet solid='False' colorPerVertex='False' coordIndex='0,4,5,-1,3,4,5,-1,2,3,4,-1,1,3,5,-1'> 

<Coordinate point='2.0 0.0 0.0,0.0 2.0 0.0,0.0 0.0 2.0,0.0 1.0 1.0,1.0 0.0 1.0,1.0 1.0 0.0'/> 

<Color color='1.0 0.0 0.0,1.0 0.6 0.0,0.8 1.0 0.0,0.2 1.0 0.0' /> 

</IndexedFaceSet> 

<BLANKLINE> 

""" 

cdef Py_ssize_t i 

points = ",".join(["%s %s %s" % (self.vs[i].x, 

self.vs[i].y, 

self.vs[i].z) 

for i from 0 <= i < self.vcount]) 

coordIndex = ",-1,".join([",".join([str(self._faces[i].vertices[j]) 

for j from 0 <= j < self._faces[i].n]) 

for i from 0 <= i < self.fcount]) 

if not self.global_texture: 

colorIndex = ",".join([str(self._faces[i].color.r) + " " 

+ str(self._faces[i].color.g) + " " 

+ str(self._faces[i].color.b) 

for i from 0 <= i < self.fcount]) 

return """ 

<IndexedFaceSet solid='False' colorPerVertex='False' coordIndex='%s,-1'> 

<Coordinate point='%s'/> 

<Color color='%s' /> 

</IndexedFaceSet> 

""" % (coordIndex, points, colorIndex) 

return """ 

<IndexedFaceSet coordIndex='%s,-1'> 

<Coordinate point='%s'/> 

</IndexedFaceSet> 

""" % (coordIndex, points) 

  

def bounding_box(self): 

r""" 

Calculate the bounding box for the vertices in this object 

(ignoring infinite or NaN coordinates). 

  

OUTPUT: 

  

a tuple ( (low_x, low_y, low_z), (high_x, high_y, high_z)), 

which gives the coordinates of opposite corners of the 

bounding box. 

  

EXAMPLES:: 

  

sage: x,y = var('x,y') 

sage: p = plot3d(sqrt(sin(x)*sin(y)), (x,0,2*pi),(y,0,2*pi)) 

sage: p.bounding_box() 

((0.0, 0.0, -0.0), (6.283185307179586, 6.283185307179586, 0.9991889981715697)) 

""" 

if self.vcount == 0: 

return ((0,0,0),(0,0,0)) 

  

cdef Py_ssize_t i 

cdef point_c low 

cdef point_c high 

  

low.x, low.y, low.z = INFINITY, INFINITY, INFINITY 

high.x, high.y, high.z = -INFINITY, -INFINITY, -INFINITY 

  

for i in range(self.vcount): 

point_c_update_finite_lower_bound(&low, self.vs[i]) 

point_c_update_finite_upper_bound(&high, self.vs[i]) 

return ((low.x, low.y, low.z), (high.x, high.y, high.z)) 

  

def partition(self, f): 

r""" 

Partition the faces of ``self``. 

  

The partition is done according to the value of a map 

`f: \RR^3 \rightarrow \ZZ` applied to the center of each face. 

  

INPUT: 

  

- `f` -- a function from `\RR^3` to `\ZZ` 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Box(1,2,3) 

sage: len(S.partition(lambda x,y,z : floor(x+y+z))) 

6 

""" 

cdef Py_ssize_t i, j, ix, face_ix 

cdef int part 

cdef point_c P 

cdef face_c *face 

cdef face_c *new_face 

cdef IndexFaceSet face_set 

  

cdef int *partition = <int *>check_allocarray(self.fcount, sizeof(int)) 

  

part_counts = {} 

for i from 0 <= i < self.fcount: 

face = &self._faces[i] 

P = self.vs[face.vertices[0]] 

for j from 1 <= j < face.n: 

point_c_add(&P, P, self.vs[face.vertices[j]]) 

point_c_mul(&P, P, 1.0/face.n) 

partition[i] = part = f(P.x, P.y, P.z) 

try: 

count = part_counts[part] 

except KeyError: 

part_counts[part] = count = [0,0] 

count[0] += 1 

count[1] += face.n 

all = {} 

for part, count in part_counts.iteritems(): 

face_set = IndexFaceSet([]) 

face_set.realloc(self.vcount, count[0], count[1]) 

face_set.vcount = self.vcount 

face_set.fcount = count[0] 

face_set.icount = count[1] 

memcpy(face_set.vs, self.vs, sizeof(point_c) * self.vcount) 

face_ix = 0 

ix = 0 

for i from 0 <= i < self.fcount: 

if partition[i] == part: 

face = &self._faces[i] 

new_face = &face_set._faces[face_ix] 

new_face.n = face.n 

new_face.vertices = &face_set.face_indices[ix] 

for j from 0 <= j < face.n: 

new_face.vertices[j] = face.vertices[j] 

face_ix += 1 

ix += face.n 

face_set._clean_point_list() 

all[part] = face_set 

sig_free(partition) 

return all 

  

def tachyon_repr(self, render_params): 

""" 

Return a tachyon object for ``self``. 

  

EXAMPLES: 

  

A basic test with a triangle:: 

  

sage: G = polygon([(0,0,1), (1,1,1), (2,0,1)]) 

sage: s = G.tachyon_repr(G.default_render_params()); s 

['TRI V0 0 0 1 V1 1 1 1 V2 2 0 1', ...] 

  

A simple colored one:: 

  

sage: from sage.plot.plot3d.index_face_set import IndexFaceSet 

sage: from sage.plot.plot3d.texture import Texture 

sage: point_list = [(2,0,0),(0,2,0),(0,0,2),(0,1,1),(1,0,1),(1,1,0)] 

sage: face_list = [[0,4,5],[3,4,5],[2,3,4],[1,3,5]] 

sage: col = rainbow(10, 'rgbtuple') 

sage: t_list=[Texture(col[i]) for i in range(10)] 

sage: S = IndexFaceSet(face_list, point_list, texture_list=t_list) 

sage: S.tachyon_repr(S.default_render_params()) 

['TRI V0 2 0 0 V1 1 0 1 V2 1 1 0', 

'TEXTURE... AMBIENT 0.3 DIFFUSE 0.7 SPECULAR 0 OPACITY 1.0... COLOR 1 0 0 ... TEXFUNC 0',...] 

""" 

cdef Transformation transform = render_params.transform 

lines = [] 

cdef point_c P, Q, R 

cdef face_c face 

cdef Py_ssize_t i, k 

sig_on() 

for i from 0 <= i < self.fcount: 

face = self._faces[i] 

if transform is not None: 

transform.transform_point_c(&P, self.vs[face.vertices[0]]) 

transform.transform_point_c(&Q, self.vs[face.vertices[1]]) 

transform.transform_point_c(&R, self.vs[face.vertices[2]]) 

else: 

P = self.vs[face.vertices[0]] 

Q = self.vs[face.vertices[1]] 

R = self.vs[face.vertices[2]] 

PyList_Append(lines, format_tachyon_triangle(P, Q, R)) 

if self.global_texture: 

PyList_Append(lines, self.texture.id) 

else: 

PyList_Append(lines, format_tachyon_texture(face.color)) 

if face.n > 3: 

for k from 3 <= k < face.n: 

Q = R 

if transform is not None: 

transform.transform_point_c(&R, self.vs[face.vertices[k]]) 

else: 

R = self.vs[face.vertices[k]] 

PyList_Append(lines, format_tachyon_triangle(P, Q, R)) 

if self.global_texture: 

PyList_Append(lines, self.texture.id) 

else: 

PyList_Append(lines, format_tachyon_texture(face.color)) 

sig_off() 

  

return lines 

  

def json_repr(self, render_params): 

""" 

Return a json representation for ``self``. 

  

TESTS: 

  

A basic test with a triangle:: 

  

sage: G = polygon([(0,0,1), (1,1,1), (2,0,1)]) 

sage: G.json_repr(G.default_render_params()) 

['{"vertices":[{"x":0,"y":0,"z":1},{"x":1,"y":1,"z":1},{"x":2,"y":0,"z":1}], "faces":[[0,1,2]], "color":"#0000ff", "opacity":1}'] 

  

A simple colored one:: 

  

sage: from sage.plot.plot3d.index_face_set import IndexFaceSet 

sage: from sage.plot.plot3d.texture import Texture 

sage: point_list = [(2,0,0),(0,2,0),(0,0,2),(0,1,1),(1,0,1),(1,1,0)] 

sage: face_list = [[0,4,5],[3,4,5],[2,3,4],[1,3,5]] 

sage: col = rainbow(10, 'rgbtuple') 

sage: t_list=[Texture(col[i]) for i in range(10)] 

sage: S = IndexFaceSet(face_list, point_list, texture_list=t_list) 

sage: S.json_repr(S.default_render_params()) 

['{"vertices":[{"x":2,"y":0,"z":0},..., "face_colors":["#ff0000","#ff9900","#cbff00","#33ff00"], "opacity":1}'] 

""" 

cdef Transformation transform = render_params.transform 

cdef point_c res 

  

if transform is None: 

vertices_str = "[{}]".format( 

",".join([format_json_vertex(self.vs[i]) 

for i from 0 <= i < self.vcount])) 

else: 

vertices_str = "[" 

for i from 0 <= i < self.vcount: 

transform.transform_point_c(&res, self.vs[i]) 

if i > 0: 

vertices_str += "," 

vertices_str += format_json_vertex(res) 

vertices_str += "]" 

  

faces_str = "[{}]".format(",".join([format_json_face(self._faces[i]) 

for i from 0 <= i < self.fcount])) 

opacity = self._extra_kwds.get('opacity', 1) 

  

if self.global_texture: 

color_str = '"#{}"'.format(self.texture.hex_rgb()) 

return ['{{"vertices":{}, "faces":{}, "color":{}, "opacity":{}}}'.format( 

vertices_str, faces_str, color_str, opacity)] 

else: 

color_str = "[{}]".format(",".join(['"{}"'.format( 

Color(self._faces[i].color.r, 

self._faces[i].color.g, 

self._faces[i].color.b).html_color()) 

for i from 0 <= i < self.fcount])) 

return ['{{"vertices":{}, "faces":{}, "face_colors":{}, "opacity":{}}}'.format( 

vertices_str, faces_str, color_str, opacity)] 

  

def obj_repr(self, render_params): 

""" 

Return an obj representation for ``self``. 

  

TESTS:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Cylinder(1,1) 

sage: s = S.obj_repr(S.default_render_params()) 

""" 

cdef Transformation transform = render_params.transform 

cdef int off = render_params.obj_vertex_offset 

cdef Py_ssize_t i 

cdef point_c res 

  

sig_on() 

if transform is None: 

points = [format_obj_vertex(self.vs[i]) for i from 0 <= i < self.vcount] 

else: 

points = [] 

for i from 0 <= i < self.vcount: 

transform.transform_point_c(&res, self.vs[i]) 

PyList_Append(points, format_obj_vertex(res)) 

  

faces = [format_obj_face(self._faces[i], off) for i from 0 <= i < self.fcount] 

if not self.enclosed: 

back_faces = [format_obj_face_back(self._faces[i], off) for i from 0 <= i < self.fcount] 

else: 

back_faces = [] 

  

render_params.obj_vertex_offset += self.vcount 

sig_off() 

  

return ["g " + render_params.unique_name('obj'), 

"usemtl " + self.texture.id, 

points, 

faces, 

back_faces] 

  

def jmol_repr(self, render_params): 

""" 

Return a jmol representation for ``self``. 

  

TESTS:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Cylinder(1,1) 

sage: S.show(viewer='jmol') # indirect doctest 

""" 

cdef Transformation transform = render_params.transform 

cdef Py_ssize_t i 

cdef point_c res 

  

self._separate_creases(render_params.crease_threshold) 

  

sig_on() 

if transform is None: 

points = [format_pmesh_vertex(self.vs[i]) 

for i from 0 <= i < self.vcount] 

else: 

points = [] 

for i from 0 <= i < self.vcount: 

transform.transform_point_c(&res, self.vs[i]) 

PyList_Append(points, format_pmesh_vertex(res)) 

  

# activation of coloring in jmol 

if self.global_texture: 

faces = [format_pmesh_face(self._faces[i], 1) 

for i from 0 <= i < self.fcount] 

else: 

faces = [format_pmesh_face(self._faces[i], -1) 

for i from 0 <= i < self.fcount] 

  

# If a face has more than 4 vertices, it gets chopped up in 

# format_pmesh_face 

cdef Py_ssize_t extra_faces = 0 

for i from 0 <= i < self.fcount: 

if self._faces[i].n >= 5: 

extra_faces += self._faces[i].n-3 

  

sig_off() 

  

all = [str(self.vcount), 

points, 

str(self.fcount + extra_faces), 

faces] 

  

from .base import flatten_list 

name = render_params.unique_name('obj') 

all = flatten_list(all) 

if render_params.output_archive: 

filename = "%s.pmesh" % (name) 

render_params.output_archive.writestr(filename, '\n'.join(all)) 

else: 

filename = "%s-%s.pmesh" % (render_params.output_file, name) 

f = open(filename, 'w') 

for line in all: 

f.write(line) 

f.write('\n') 

f.close() 

  

if self.global_texture: 

s = 'pmesh {} "{}"\n{}'.format(name, filename, 

self.texture.jmol_str("pmesh")) 

else: 

s = 'pmesh {} "{}"'.format(name, filename) 

  

# Turn on display of the mesh lines or dots? 

if render_params.mesh: 

s += '\npmesh %s mesh\n' % name 

if render_params.dots: 

s += '\npmesh %s dots\n' % name 

return [s] 

  

def dual(self, **kwds): 

""" 

Return the dual. 

  

EXAMPLES:: 

  

sage: S = cube() 

sage: T = S.dual() 

sage: len(T.vertex_list()) 

6 

  

""" 

cdef point_c P 

cdef face_c *face 

cdef Py_ssize_t i, j, ix, ff 

cdef IndexFaceSet dual = IndexFaceSet([], **kwds) 

cdef int incoming, outgoing 

cdef dict dd 

  

dual.realloc(self.fcount, self.vcount, self.icount) 

  

# is using dicts overly-heavy? 

dual_faces = [{} for i from 0 <= i < self.vcount] 

  

for i from 0 <= i < self.fcount: 

sig_check() 

# Let the vertex be centered on the face according to a simple average 

face = &self._faces[i] 

dual.vs[i] = self.vs[face.vertices[0]] 

for j from 1 <= j < face.n: 

point_c_add(&dual.vs[i], dual.vs[i], self.vs[face.vertices[j]]) 

point_c_mul(&dual.vs[i], dual.vs[i], 1.0/face.n) 

  

# Now compute the new face 

for j from 0 <= j < face.n: 

if j == 0: 

incoming = face.vertices[face.n-1] 

else: 

incoming = face.vertices[j-1] 

if j == face.n-1: 

outgoing = face.vertices[0] 

else: 

outgoing = face.vertices[j+1] 

dd = dual_faces[face.vertices[j]] 

dd[incoming] = i, outgoing 

  

i = 0 

ix = 0 

for dd in dual_faces: 

sig_check() 

face = &dual._faces[i] 

face.n = len(dd) 

if face.n == 0: # skip unused vertices 

continue 

face.vertices = &dual.face_indices[ix] 

ff, next_ = next(dd.itervalues()) 

face.vertices[0] = ff 

for j from 1 <= j < face.n: 

ff, next_ = dd[next_] 

face.vertices[j] = ff 

i += 1 

ix += face.n 

  

dual.vcount = self.fcount 

dual.fcount = i 

dual.icount = ix 

  

return dual 

  

def stickers(self, colors, width, hover): 

""" 

Return a group of IndexFaceSets. 

  

INPUT: 

  

- ``colors`` -- list of colors/textures to use (in cyclic order) 

  

- ``width`` -- offset perpendicular into the edge (to create a border) 

may also be negative 

  

- ``hover`` -- offset normal to the face (usually have to float above 

the original surface so it shows, typically this value is very 

small compared to the actual object 

  

OUTPUT: 

  

Graphics3dGroup of stickers 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.shapes import Box 

sage: B = Box(.5,.4,.3, color='black') 

sage: S = B.stickers(['red','yellow','blue'], 0.1, 0.05) 

sage: S.show() 

sage: (S+B).show() 

  

""" 

all = [] 

n = self.fcount 

ct = len(colors) 

for k in range(len(colors)): 

if colors[k]: 

all.append(self.sticker(list(xrange(k, n, ct)), width, hover, 

texture=colors[k])) 

return Graphics3dGroup(all) 

  

def sticker(self, face_list, width, hover, **kwds): 

""" 

Return a sticker on the chosen faces. 

""" 

if not isinstance(face_list, (list, tuple)): 

face_list = (face_list,) 

faces = self.face_list() 

all = [] 

for k in face_list: 

all.append(sticker(faces[k], width, hover)) 

return IndexFaceSet(all, **kwds) 

  

  

cdef class FaceIter: 

""" 

A class for iteration over faces 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Box(1,2,3) 

sage: len(list(S.faces())) == 6 # indirect doctest 

True 

""" 

def __init__(self, face_set): 

""" 

""" 

self.set = face_set 

self.i = 0 

  

def __iter__(self): 

return self 

  

def __next__(self): 

cdef point_c P 

if self.i >= self.set.fcount: 

raise StopIteration 

else: 

face = [] 

for j from 0 <= j < self.set._faces[self.i].n: 

P = self.set.vs[self.set._faces[self.i].vertices[j]] 

PyList_Append(face, (P.x, P.y, P.z)) 

self.i += 1 

return face 

  

  

cdef class EdgeIter: 

""" 

A class for iteration over edges 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Box(1,2,3) 

sage: len(list(S.edges())) == 12 # indirect doctest 

True 

""" 

def __init__(self, face_set): 

self.set = face_set 

if not self.set.enclosed: 

raise TypeError("Must be closed to use the simple iterator.") 

self.i = 0 

self.j = 0 

self.seen = {} 

  

def __iter__(self): 

return self 

  

def __next__(self): 

cdef point_c P, Q 

cdef face_c face = self.set._faces[self.i] 

while self.i < self.set.fcount: 

if self.j == face.n: 

self.i += 1 

self.j = 0 

if self.i < self.set.fcount: 

face = self.set._faces[self.i] 

else: 

if self.j == 0: 

P = self.set.vs[face.vertices[face.n-1]] 

else: 

P = self.set.vs[face.vertices[self.j-1]] 

Q = self.set.vs[face.vertices[self.j]] 

self.j += 1 

if self.set.enclosed: # Every edge appears exactly twice, once in each orientation. 

if point_c_cmp(P, Q) < 0: 

return ((P.x, P.y, P.z), (Q.x, Q.y, Q.z)) 

else: 

if point_c_cmp(P, Q) > 0: 

P,Q = Q,P 

edge = ((P.x, P.y, P.z), (Q.x, Q.y, Q.z)) 

if not edge in self.seen: 

self.seen[edge] = edge 

return edge 

raise StopIteration 

  

  

cdef class VertexIter: 

""" 

A class for iteration over vertices 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.shapes import * 

sage: S = Box(1,2,3) 

sage: len(list(S.vertices())) == 8 # indirect doctest 

True 

""" 

def __init__(self, face_set): 

self.set = face_set 

self.i = 0 

  

def __iter__(self): 

return self 

  

def __next__(self): 

if self.i >= self.set.vcount: 

raise StopIteration 

else: 

self.i += 1 

return (self.set.vs[self.i-1].x, self.set.vs[self.i-1].y, self.set.vs[self.i-1].z) 

  

  

def len3d(v): 

""" 

Return the norm of a vector in three dimensions. 

  

EXAMPLES:: 

  

sage: from sage.plot.plot3d.index_face_set import len3d 

sage: len3d((1,2,3)) 

3.7416573867739413 

""" 

return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]) 

  

  

def sticker(face, width, hover): 

""" 

Return a sticker over the given face. 

""" 

n = len(face) 

edges = [] 

for i from 0 <= i < n: 

edges.append(vector(RDF, [face[i-1][0] - face[i][0], 

face[i-1][1] - face[i][1], 

face[i-1][2] - face[i][2]])) 

sticker = [] 

for i in range(n): 

v = -edges[i] 

w = edges[i - 1] 

N = v.cross_product(w) 

lenN = N.norm() 

dv = v * (width * w.norm() / lenN) 

dw = w * (width * v.norm() / lenN) 

sticker.append(tuple(vector(RDF, face[i-1]) + dv + dw + N*(hover/lenN))) 

return sticker